The user should give a string, where we should check if all characters are numbers.
If all are digits, we will get one message that the password is with digits and move on with the program. If any of them (or all) is not a digit, then the user should enter password again.
(E.g. 465486
is acceptable. hell3429384
or sfdkjsfkj
is not acceptable)
Apparently, my code is not working correctly, since whether I give a number or a letter to the password, I get the same result.
Am I looping something wrong, or am I using the isDigit
method wrong?
import java.util.*;
public class Password {
public static void main(String[] args) {
Scanner passn = new Scanner(System.in);
System.out.println("Give password: ");
String pass = passn.nextLine(); /* user enters password*/
int length = pass.length();
boolean isadigit;
for(int i = 0; i < length; i++) { /*looping each character one by one*/
char character= pass.charAt(i);
if (Character.isDigit(i)) { /*checking if each character is digit*/
isadigit = true; /* if it is, give the value to isadigit true*/
System.out.println("PASSWORD is w digits");
}
else {
System.out.println("PASSWORD w/out digits");
passn.nextLine();
}
}
}
}