java error cannot find symbol in last ifelse statement becau
java error: cannot find symbol in last if/else statement because out of scope
import java.util.*;
public class A9_P2_Yang_Lucy
{
public static void main(String[] args)
{
//declare variables
String input;
int passLength;
boolean hasUpperLetters=false;
boolean hasLowerLetters=false;
boolean hasDigits=false;
boolean hasSomethingElse=false;
//Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
//Ask the user to enter their password
System.out.print(\"Please enter your password: \");
input = keyboard.nextLine();
passLength=input.length();
//loop repeats for all characters in string
for (int i=0; i<passLength; i++)
{
char c=input.charAt(i);
//checks for atleast one upper case character
if(Character.isUpperCase(c))
hasUpperLetters=true;
//checks for atleast one lower case character
else if(Character.isLowerCase(c))
hasLowerLetters=true;
//checks for atleast one digit
else if(Character.isDigit(c))
hasDigits=true;
else hasSomethingElse=true;
}
//condition to check for password verified
if (hasUpperCaseLetters && hasDigits && hasLowerCaseLetters && !hasSomethingElse && (passLength>=6))
{
System.out.println(\"password is correctly formatted\");
}
else
{
System.out.println(\"password is not correctly formatted\");
}
}
}
PLEASE CHECK MY OTHER JAVA QUESTIONS TOO
Solution
Hi,
I have fixed the issue and highlighted the code changes below..
A9_P2_Yang_Lucy.java
import java.util.*;
public class A9_P2_Yang_Lucy
{
public static void main(String[] args)
{
//declare variables
String input;
int passLength;
boolean hasUpperLetters=false;
boolean hasLowerLetters=false;
boolean hasDigits=false;
boolean hasSomethingElse=false;
//Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
//Ask the user to enter their password
System.out.print(\"Please enter your password: \");
input = keyboard.nextLine();
passLength=input.length();
//loop repeats for all characters in string
for (int i=0; i<passLength; i++)
{
char c=input.charAt(i);
//checks for atleast one upper case character
if(Character.isUpperCase(c))
hasUpperLetters=true;
//checks for atleast one lower case character
else if(Character.isLowerCase(c))
hasLowerLetters=true;
//checks for atleast one digit
else if(Character.isDigit(c))
hasDigits=true;
else hasSomethingElse=true;
}
//condition to check for password verified
if (hasUpperLetters && hasDigits && hasLowerLetters && !hasSomethingElse && (passLength>=6))
{
System.out.println(\"password is correctly formatted\");
}
else
{
System.out.println(\"password is not correctly formatted\");
}
}
}
Output:
Please enter your password: Sures1
password is correctly formatted
Please enter your password: abcfd
password is not correctly formatted

