+ 2
[SOLVED] Does anyone know why my code is failing the test cases 10 and 11 (password validation)
Does anyone know why the code is failing test cases #10/11 or what their input is? import java.util.Scanner; public class Program { public static void main(String[] args) { int numberOfSpecialLetters = 0; int numberOfNumbers = 0; String[] specialLetters = { "!", "@", "#", "
quot;, "%", "&", "*" }; String[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; Scanner in = new Scanner(System.in); String password = in.nextLine(); int length = password.length(); for (String specialLetter : specialLetters) { if (password.contains(specialLetter)) { numberOfSpecialLetters++; } } for (String number : numbers) { if (password.contains(number)) { numberOfNumbers++; } } if (numberOfSpecialLetters >= 2 && numberOfNumbers >= 2 && length >= 7) { System.out.println("Strong"); } else { System.out.println("Weak"); } } }12 odpowiedzi
+ 2
Check out this 👇
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
String password;
int count1=0;
int count2=0;
Scanner s = new Scanner(System.in);
password=s.nextLine();
for(int x=0;x<=(password.length()-1);x++)
{
Boolean number=Character.isDigit(password.charAt(x));
if(number)
{
count1++;
}
else
{
Boolean Letter=Character.isLetter(password.charAt(x));
if(Letter)
{
}
else
{
count2++;
}
}
}
if((password.length() >= 7) && (count1 >= 2) && (count2 > 0))
{
System.out.println("Strong");
}
else
{
System.out.println("Weak");
}
}
}
0
Quantum There must be a test case but may not have other constrains satisfies I think.
Yes. It output Weak but it is a Strong valid password according to description. It should output Strong.
Ok.Let wait for OP response, for his changes in code .
0
Jayakrishna🇮🇳 was right. It the code checked only once for every special character or number.
Instead of checking the password as string against a string array of special signs and numbers, I reversed it and turned the password into a string array and had a string each of special signs and numbers checked against it in the for each loop.
Thank you for your help guys!
import java.util.Arrays;
import java.util.Scanner;
public class PasswordValidation {
public static void main(String[] args) {
int numberOfSpecialLetters = 0;
int numberOfNumbers = 0;
String specialLetters = "!@#$%&*";
String neededNumbers = "0123456789";
Scanner in = new Scanner(System.in);
String password = in.nextLine();
int length = password.length();
String[] passwordAsString = password.split("");
for (String specialLetter : passwordAsString) {
if (specialLetters.contains(specialLetter)) {
numberOfSpecialLetters++;
}
}
for (String number : passwordAsString) {
if (neededNumbers.contains(number)) {
numberOfNumbers++;
}
}
if (numberOfSpecialLetters >= 2 && numberOfNumbers >= 2 && length >= 7) {
System.out.println("Strong");
} else {
System.out.println("Weak");
}
}
}
0
Markus Gebhard yes. Reversed way will be the correct solution to get it right. hope it solved.
You're welcome...
0
Quantum, the code passed all test cases.
>=7 is correct
0
import java.util.Scanner ;
import java.io.*;
public class Program
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String str=input.nextLine();
int n=0;
int s=0;
int l=0;
for(int i=0;i<str.length();i++){
if(Character .isDigit (str.charAt (i)))
n++;
else if(!Character .isDigit (str.charAt (i))&&!Character.isLetter(str.charAt (i))&&!Character.isWhitespace(str.charAt (i)) )
s++;
else if(Character.isLetter(str.charAt (i)))
l++;
}
int t=n+s+l;
boolean flag=false;
if(t>6)
{
if(n>1&&s>1&&l>2)
flag=true;
if(flag)
System .out. println ("Strong");
else
System .out .println ("Weak");
}
else {
System .out .println ("Weak");
}
}
}
0
You will pass all test cases
- 1
Just tried. Doesn't make a difference
- 1
length>=7 is the correct condition
But your code not work if input has duplicates characters, since you are checking only once.
For ex: abc11@@ is Strong valid password but your code giving weak password...
- 1
Quantum
Even though test cases passed or not, length>=7 is correct condition. Check again that there said password length must be atleast 7 charecters. It means length should be 7 or more characters..
And next is what I said is same as you posted in your next post. again..
Check again..
- 1
Quantum If you not count 100%, then your code also not pass 100%. Means test cases are hidden, then how can you expect there may not case length==7.
This is simple problem, if you not count 100% ,then in bigger projects you phase many problems there counting 100% is very tough.
Ok. thats all individual opinions. Let it be.
Just say what it means in code
"password must be atleast 7 charecters length"
- 1
Quantum We are here on Sololearn to learn how to lead in the real world.
The Task : where is the proof? So you do all opposite with that example. Anyways that's not our task now.
Just read again my posts. And say who is being so stubborn..? I said what is correct . Not said what is wrong. (Not address you., But I can . Because there is wrong answer). Then you started discussion on my reply and example. You said its wrong example but it's a perfectly correct example..
I already said ,let it be. I just want to respond the questioner. I replied only to all your posts. Not started questions. But you keep on replying... even you down voted but not iam. still not. So stop it now.. This is my last reply .. let me do my discussion with op.