SO my progrm is supose to read hte users password and then s

 SO my progrm is supose to read hte users password and then say if it is valid or invaild and then rerun but the second or third time it is run it says the input is invalid even if it is valid  help!   import java.util.Scanner; public class PasswordTest {     public static void main(String[] args)     {         Scanner scanner = new Scanner(System.in);          //variables         boolean size;         boolean lowCase;         boolean upperCase;         boolean special;         boolean digit;         int count = 0;         char again = \'Y\';         String extra;          //while statement to run prgram again         while(again == \'Y\') {              //user enter password             System.out.print(\"Enter password:\");             String password = scanner.nextLine();              //variables             size = false;             lowCase = false;             upperCase = false;             special = false;             digit = false;             int length = password.length();              //while loop to validate password             while (count < length) {                 char ch = password.charAt(count);                  if (length >= 8) {                     size = true;                 }                 if (Character.isLowerCase(ch)) {                     lowCase = true;                 }                 if (Character.isUpperCase(ch)) {                     upperCase = true;                 }                 if (!Character.isLetterOrDigit(ch)) {                     special = true;                 }                 if (Character.isDigit(ch)) {                     digit = true;                 }                 count++;             }             //print out users password             System.out.println(\"Entered Password:\" + password);              //figure out if password is valid or not             if (size && lowCase && upperCase && special && digit) {                 System.out.println(\"Verdict: \\t\\t Valid\");             } else {                 System.out.println(\"Verdict: \\t\\t Invalid\");             }             //ask user if they want to check password again             System.out.print(\"Would you like to check password again? enter Y for yes or N for no: \");             again = scanner.next().charAt(0);             again = Character.toUpperCase(again);              extra = scanner.nextLine();         }     } } 

Solution

//The entire code is correct, except for the fact that you did not set count inside the while loop to zero. Hence //the wrong answer was being given.

//Check the line where to include the count statement. I have commented it as well.

import java.util.Scanner;
public class PasswordTest
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

//variables
boolean size;
boolean lowCase;
boolean upperCase;
boolean special;
boolean digit;
int count = 0;
char again = \'Y\';
String extra;

//while statement to run prgram again
while(again == \'Y\') {

//user enter password
System.out.println(\"Enter password:\");
String password = scanner.nextLine();

//variables
size = false;
lowCase = false;
upperCase = false;
special = false;
digit = false;
int length = password.length();
count=0; //Here the count has to be set to 0 again.
//while loop to validate password
while (count < length) {
char ch = password.charAt(count);

if (length >= 8) {
size = true;
}
if (Character.isLowerCase(ch)) {
lowCase = true;
}
if (Character.isUpperCase(ch)) {
upperCase = true;
}
if (!Character.isLetterOrDigit(ch)) {
special = true;
}
if (Character.isDigit(ch)) {
digit = true;
}
count++;
}
//print out users password
System.out.println(\"Entered Password:\" + password);

//figure out if password is valid or not
if (size && lowCase && upperCase && special && digit) {
System.out.println(\"Verdict: \\t\\t Valid\");
} else {
System.out.println(\"Verdict: \\t\\t Invalid\");
}
//ask user if they want to check password again
System.out.println(\"Would you like to check password again? enter Y for yes or N for no: \");
again = scanner.next().charAt(0);
again = Character.toUpperCase(again);

extra = scanner.nextLine();
}
}
}

 SO my progrm is supose to read hte users password and then say if it is valid or invaild and then rerun but the second or third time it is run it says the inpu
 SO my progrm is supose to read hte users password and then say if it is valid or invaild and then rerun but the second or third time it is run it says the inpu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site