Reputation: 13
I don't know why my if else condition not working properly. it'll print "Try again" no matter what(input correct or wrong data).please help. thankyou so much in advance.
public class InventoryManagementSystem {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
Administrator obj = new Administrator();
String str = "123";
System.out.print("Enter user ID: ");
String userID = input.nextLine();
System.out.print("Enter user password: ");
int userPassword = Integer.parseInt(str);
str = input.nextLine();
System.out.print("Enter user Phone Number: ");
long phoneNo = Integer.parseInt(str);
str = input.nextLine();
Administrator obj1 = new Administrator(userID, userPassword);
Administrator obj2 = new Administrator(userID, userPassword, phoneNo);
User obj3 = new User();
System.out.println("ID : ");
String ID = input.nextLine();
System.out.println("Password : ");
int pass = Integer.parseInt(str);
str = input.nextLine();
User obj4 = new User(ID,pass);
if (userPassword==pass && userID.equals(ID)){
System.out.println("Login succesfully!");
}
else {
System.out.println("Try Again");
}
}
}
Upvotes: 0
Views: 44
Reputation: 302
You are always parsing userPassword with default value "123".
Working Code:
Scanner input = new Scanner(System.in);
Administrator obj = new Administrator();
String str = "123";
System.out.print("Enter user ID: ");
String userID = input.nextLine();
System.out.print("Enter user password: ");
str = input.nextLine();
int userPassword = Integer.parseInt(str);
System.out.print("Enter user Phone Number: ");
str = input.nextLine();
long phoneNo = Integer.parseInt(str);
Administrator obj1 = new Administrator(userID, userPassword);
Administrator obj2 = new Administrator(userID, userPassword, phoneNo);
User obj3 = new User();
System.out.println("ID : ");
String ID = input.nextLine();
System.out.println("Password : ");
int pass = Integer.parseInt(str);
str = input.nextLine();
User obj4 = new User(ID, pass);
if (userPassword == pass && userID.equals(ID)) {
System.out.println("Login succesfully!");
} else {
System.out.println("Try Again");
}
Upvotes: 0
Reputation: 2054
You have the following lines out of order throughout the code.
This :-
System.out.println("Password : ");
int pass = Integer.parseInt(str);
str = input.nextLine();
should be this :-
System.out.println("Password : ");
str = input.nextLine();
int pass = Integer.parseInt(str);
Upvotes: 1