BlueJ will not return \"true\" nor \"false\" when running this code.... import j
ID: 3628077 • Letter: B
Question
BlueJ will not return "true" nor "false" when running this code....import java.util.*;
public class pw
{
public static boolean isPasswordCorrect(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'm', 'y', 'p', 'a', 's', 's', 'w' };
if (input.length != correctPassword.length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword);
}
//Zero out the password.
Arrays.fill(correctPassword,'0');
return isCorrect;
}
public static void main(String[] args)
{
Scanner myinput = new Scanner(System.in);
String userinput = myinput.next();
//declare the char array
char[] stringArray;
//convert string into array using toCharArray() method of string class
stringArray = userinput.toCharArray();
System.out.print(isPasswordCorrect(stringArray));
}
}
Explanation / Answer
It executes properly for me in Eclipse and at the command line. Seems like an issue with BlueJ. Try to capitalize the class name, maybe that will fix it. Classes, by convention, should always be capitalized. Also, try a prompt before accepting input. You could also debug it by printing to System.out in other parts of the program to find where your problem is. ex) System.out.print("Enter Password: ");
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.