Reputation: 75
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.
Upvotes: 0
Views: 212
Reputation: 1188
Change
if(user!="admin" && pass!="admin")
to if(user!="admin" || pass!="admin")
If you wan to check for Invalid username or password
Upvotes: 3
Reputation: 485
If I understand your desire out of the code, there are a few critical errors:
Firstly, if the username and password "admin" are correct, and you want to end the loop, the code should be:
System.out.print("Success!");
tryAgain = false;
not:
System.out.print("Success!");
tryAgain = true;
Setting tryAgain to false means that (as your variable (and later code) is written) the program will not "try again" for the user's name and password (the loop will stop looping).
Also, you have the same discrepancy in the following statement:
if(user!="admin" && pass!="admin"){
...
tryAgain = false;
tryAgain should be set equal to true here, as you want to program to "try again" for an input name and password (tryAgain = true
).
Lastly, and most importantly, your loop while command isn't actually doing anything. This:
while(tryAgain = true);
is defining a variable, not reading information from it. You must write:
while(tryAgain == true);
if you want to check if tryAgain is set to true.
Besides all that, there are a few bad coding style choices here:
Your two if statements could be combined into:
if(user.equals("admin") && pass.equals("admin")){
System.out.print("Success!");
tryAgain = false;
}
else{
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = true;
}
As there is only one way to succeed, there is no reason to not lump everything else into an else clause.
And the while statement at the end can simply be written as:
while(tryAgain)
and while tryAgain is true, it will loop.
The other issue here, is if the user just clicks enter and pass is read as an empty string. As the program tries to compare the string to "admin" with nothing it will throw back an exception. I would add an initial:
if(user.isEmpty() || pass.isEmpty()){
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = false
}
to the if statement.
Here is what I would do with the code:
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 = true;
do{
System.out.print("\nInput username: ");
String user = inp.nextLine();
System.out.print("\nInput password: ");
String pass = inp.nextLine();
if(user.isEmpty() || pass.isEmpty()){
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = false
}
else if(user.equals("admin") && pass.equals("admin")){
System.out.print("Success!");
tryAgain = false;
}
else{
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = true;
}
}while(tryAgain);
}
}
I certainly hope that helps and I was able to sort out some confusion!
Upvotes: 0
Reputation: 5684
Try it this way:
public static void main(String[]args){
Scanner inp = new Scanner(System.in);
boolean tryAgain = true;
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 = false;
}
if(!user.equals("admin") || !(pass.equals("admin")){
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = true;
}
}while(tryAgain);
}
}
Upvotes: 4
Reputation: 1212
Check the below solutions.
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.equals("admin") || !pass.equals("admin")){
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = false;
}
}while(tryAgain);
}
}
You are comparing the two string object with != . That is wrong.
Problem line in your post is: if(user!="admin" && pass!="admin"){
Upvotes: 0
Reputation: 24
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
boolean tryAgain = false;
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;
inp.close();
} else {
JOptionPane.showMessageDialog(null,
"Try again! Invalid username or password!",
"Error Logging-In", JOptionPane.ERROR_MESSAGE);
}
} while (tryAgain == false);
}
Upvotes: 0
Reputation: 20741
use equals instead of == and
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( !"admin".equals(user) && !"admin".equals(pass)){
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = false;
}
}while(tryAgain = true);
Upvotes: 0
Reputation: 10665
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 = false;
}
else
{
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = true;
}
} while(tryAgain == true);
}
}
There have been a number of changes made.
Firstly, your code has been indented properly - it makes it a lot easier for anyone wanting to help!
Secondly, you don't need to have two if statements. You can have an if-else statement because you want to do this: "if the user has the correct name and password, do this. if the user does not have the correct name and password, do this." which can be shortened to "if you user has the correct name and password, do this. if not, do this.".
Thirdly, your do-while loop will continue when tryAgain is true, and so instead of while(tryAgain = true)
, you need while(tryAgain == true)
, because two equals signs are for comparison whereas a single one is not.
Fourthly, your loop continues whilst tryAgain is true, and so you want tryAgain to be true if they enter the invalid username or password, to continue the loop, and false if they enter the correct one, to stop the loop.
Also, don't use == to compare strings. == checks if two objects are the same, so user will never == "admin". Instead, use the equals() method to compare strings.
Upvotes: 0
Reputation: 94459
Set tryAgain
to false after success to break the loop. Also utilize an else if
after checking to see if the user has successfully logged in to skip the check for an invalid user. Also as others mention the comparison of String
objects should use the equals
method. Finally, the while loop should use the comparison operator ==
instead of the assignment operator =
.
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 = false; //Changed to false to break loop
}else if (!user.equals("admin") && !pass.equals("admin")) {
//^Using equals instead of ==, added else if
JOptionPane.showMessageDialog(null,
"Try again! Invalid username or password!",
"Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = false;
}
} while (tryAgain == true); //using == instead of =
}
Upvotes: 1
Reputation: 806
why not just use else and also tryAgain is used wrongly like this
if(user.equals("admin") && pass.equals("admin")){
System.out.print("Success!");
tryAgain = false; //why repeat again
}
else{
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = true; //ask again
}
also it whould be
while(tryAgain)
Upvotes: 0
Reputation: 12843
while(tryAgain == true)
change = to ==
=
is for assigning value.
==
is for checking condition.
You can also use .
while(tryAgain)
Upvotes: 2
Reputation: 425003
This assigns true
to tryAgain
(which will always evaluate as true
, creating an infinite loop):
} while(tryAgain = true)
So it should be:
} while(tryAgain == true)
But the whole problem could have been avoided by following good coding style; it should be simply:
} while(tryAgain)
Never compare a boolean variable with a boolean constant, just use booleanVar
or !booleanVar
as your condition
Upvotes: 2
Reputation: 4956
There are couple mistakes in your code:
if(user!="admin" && pass!="admin"){ .... tryAgain = false;
Should be:
if(user!="admin" || pass!="admin"){ .... tryAgain = true;
Then:
System.out.print("Success!");
tryAgain = true;
Should be:
System.out.print("Success!");
tryAgain = false;
And finally:
while(tryAgain = true);
Should be:
while(tryAgain == true);
or just while(tryAgain);
Hope that helps!
Upvotes: 0
Reputation: 40995
Try this:
public class Wewe{
public static void main(String[]args){
Scanner inp = new Scanner(System.in);
boolean tryAgain = true;
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 = false;
}
if(user!="admin" && pass!="admin"){
JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
tryAgain = true;
}
} while(tryAgain);
}
}
Upvotes: 1