I am having a problem with a do-while loop. It has two if statements inside of it. The program is supposed to take your username and password (which you enter) and then confirm them by having you type them in again. When you type them again, the have to be the same as the first time you typed them. The do-while loop is supposed to stop when boolean redo is set to false, (it gets set to false when you re-enter your username and password correctly) however the loop keeps going, even though it says that you got the username and password correct. (It says Welcome, (Username)) then the loop goes again and asks you to re-enter your username and password. How can I stop this loop after getting the correct password? Please help.
package Pack1;
import java.util.Scanner;
public class class1 {
public static void main(String[] args){
String Username; //Used to set the original username
String Password; //Used to set the original password
String Usernameuse; //Used as a test. This one has to be equal to the original username.
String Passworduse; //Used as a test. This one has to be equal to the original password.
boolean redo; //This is to determine whether the do-while loop will repeat.
Scanner in1 = new Scanner(System.in); //getting the original username
System.out.println("Enter your desired username");
Username = in1.nextLine();
Scanner in2 = new Scanner(System.in); //getting original password
System.out.println("Enter your desired password");
Password = in2.nextLine();
System.out.println("Identity Confirmation-- Enter your account information");
do{
Scanner in3 = new Scanner(System.in); //gets second username which has to be equal to original
System.out.println("Please Enter your Username");
Usernameuse = in3.nextLine();
Scanner in4 = new Scanner(System.in); //gets second password which has to be equal to the original
System.out.println("Please Enter your Password");
Passworduse = in4.nextLine();
if(Usernameuse.equals(Username) && Passworduse.equals(Password)){ //determines if both are true
System.out.println("Welcome, " + Username);
redo = false; //makes redo = false
}
if(!Usernameuse.equals(Username) || !Passworduse.equals(Password)){ //determines if either one is false
System.out.println("Either Username or Password are incorrect, please redo");
redo = true; //makes redo = true
}
} while(redo = true); //Is supposed to stop looping when you set redo to false, by entering correct username and password
System.out.println("You are now on your secret account!");
}
}
while(redo = true);
This is an assignment instead of a comparison. This will always be true
.
while(redo == true);
is what you meant to type, but
while(redo);
is what you really want because it makes it impossible to commit the assignment-instead-of-comparison error.
When you compare a constant other than a boolean
and a variable it's always best to put the constant first for the same reason.
if (1 == someInt)
instead of
if (someInt == 1)
If you accidentally use =
instead of ==
the constant-first form won't compile.
while(redo = true)
Results in always true
because it is equal to while(true)
.
=
is assignment
==
is comparison.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.