|
Hello everyone! I'm currently having a problem with my program it doesn't loop properly please help me with it. The code is below. Thanks in advance!
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.*;
public class Wewe{
public static void main(String[]args){
Scanner inp = new Scanner(System.in);
boolean tryAgain;
do{
System.out.print("\nInput username: ");
String user = inp.nextLine();
System.out.print("\nInput password: ");
String pass = inp.nextLine();
if(user.equals("admin") && pass.equals("admin")){
System.out.print("Success!");
tryAgain = true;
}
if(user!="admin" && pass!="admin"){
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = false;
}
}while(tryAgain = true);
}
}
What I want to happen is that once the user entered wrong username or password the program will then loop. But if the user entered the correct username or password, it wont loop asking the user for the correct one.
|
|
|
|
|
}while(tryAgain = true);
The above code will always set tryAgain to true ; you should be using the logical == operator.
|
|
|
|
|
I'm new on this site but i know Core Java And Adv. Java very well.. So here is solution to u r problem if u have not got it yet.
First use assigment operator == in while loop
In your code loop will keep executing till the value of tryAgain is true . if u want loop to stop right after correct value of username and password u need to make tryAgain false in first if block and make tryAgain true in second if block. it will work properly
|
|
|
|
|
Please don't use childish txtspk (u ur ...) in responses. It takes very little effort to spell words in full.
|
|
|
|
|
hai my friend..
my suggestion is, i think you are change the while condition into
while(tryAgain != true);
please try it..if it works pls no problem. if its worng sorry for it..thank you..
anything will get,just put your hardwork..
Senthilkanth..
|
|
|
|