I have problem. I get this exercise but idk how i can resolve this last three solution in my code. I need print FAIL but i cant get because i only get OK OK i thats all.
package NewPackage;
/*
password = 123
checkPassword(123) -> OK OK PASS
checkPassword(124) -> OK OK FAIL
checkPassword(12) -> OK OK FAIL
checkPassword(1234) -> OK OK OK FAIL
checkPassword(81234) -> FAIL
*/
public class Tworzenie_tablicy_char {
public static void main(String[] args) {
String password = "123";
String checkPassword = "12";
char[] charPassword = new char[password.length()];
char[] charCheckPassword = new char[checkPassword.length()];
for (int i = 0; i < password.length(); i ) {
charPassword[i] = password.charAt(i);
//System.out.println(password.charAt(i));
}
for (int i = 0; i < checkPassword.length(); i ) {
charCheckPassword[i] = checkPassword.charAt(i);
//System.out.println(checkPassword.charAt(i));
}
int minLength = Math.min(charPassword.length, charCheckPassword.length);
for (int i = 0; i < minLength; i ) {
boolean correctly = charPassword[i] == charCheckPassword[i];
if (correctly == true) {
System.out.println("OK");
} else {
System.out.println("FAIL");
}
}
}
}
Any suggestions ??
CodePudding user response:
there is no need to convert to char string, you can use charAt
to get characters. Take the min length and check if both chars at the same index are equal and then start from the index at the length of the min to the index in the length of the max and print FAIL
public class Tworzenie_tablicy_char {
public static void main(String[] args) {
String password = "123";
String checkPassword = "12";
int minLength = Math.min(password.length(), checkPassword.length());
int maxLength = Math.max(password.length(), checkPassword.length());
for (int i = 0; i < minLength; i ) {
if (password.charAt(i) == checkPassword.charAt(i))
System.out.println("OK");
else
System.out.println("FAIL");
}
for(int i = minLength;i<maxLength;i )
System.out.println("FAIL");
}
}
the output
java -cp /tmp/Jk7ZebI9iH Tworzenie_tablicy_char
OK
OK
FAIL
CodePudding user response:
I get this solution but idk its correctly, i think i can get better and clean solution. :/
package NewPackage;
/*
password = 123
checkPassword(123) -> OK OK PASS
checkPassword(124) -> OK OK FAIL
checkPassword(12) -> OK OK FAIL
checkPassword(1234) -> OK OK OK FAIL
checkPassword(81234) -> FAIL
*/
public class Tworzenie_tablicy_char {
public static void main(String[] args) {
String password = "123";
String checkPassword = "12";
char[] charPassword = new char[password.length()];
char[] charCheckPassword = new char[checkPassword.length()];
for (int i = 0; i < password.length(); i ) {
charPassword[i] = password.charAt(i);
//System.out.println(password.charAt(i));
}
for (int i = 0; i < checkPassword.length(); i ) {
charCheckPassword[i] = checkPassword.charAt(i);
//System.out.println(checkPassword.charAt(i));
}
int minLength = Math.min(charPassword.length, charCheckPassword.length);
if (charPassword.length < charCheckPassword.length)
{
System.out.println("FAIL");
}else {
for (int i = 0; i < minLength; i ) {
boolean correctly = charPassword[i] == charCheckPassword[i];
if (correctly == true) {
System.out.println("OK");
} else {
System.out.println("FAIL");
}
}
if (charPassword.length != charCheckPassword.length) {
System.out.println("FAIL");
}
}
}
}
CodePudding user response:
to compare any methods, use
method.equals(method you want to compare);
if I did not understand this question, please let me know
CodePudding user response:
I need to compare two passwords, one is String password and the other is String checkPassword. The program is to compare each letter or number separately, if it is the same it should print OK, so if it compares the password 123 to 123 it should write OK OK PASS, if 123 to 124 then OK OK FAIL, if 123 to 12 then OK OK FAIL, if 123 to 1234 is OK OK OK FAIL, and if 123 to 81234 it should throw FAIL.