Why is my code saying the input is invalid when I enter the correct numbers? imp
ID: 3716788 • Letter: W
Question
Why is my code saying the input is invalid when I enter the correct numbers?
import javax.swing.JOptionPane;
public class BankAccount
{
public static void main (String [] args)
{
//get the user to either open an account, or log in
String userOption;
int userNum;
userOption = JOptionPane.showInputDialog("Welcome! If you already have an account and would like to log in, please type '1'. If you would like to create an account, please type '2'.");
//convert string userOption to an int userNum
userNum = Integer.parseInt(userOption);
//validate input
if (userNum != 1 || userNum != 2)
{
JOptionPane.showInputDialog("Invalid input. Please type '1' to log in. To create a new account, type '2'.");
}
JOptionPane.showMessageDialog(null,"Good bye");
System.exit(0);
}
}
Explanation / Answer
BankAccount.java
import javax.swing.JOptionPane;
public class BankAccount
{
public static void main (String [] args)
{
//get the user to either open an account, or log in
String userOption;
int userNum;
userOption = JOptionPane.showInputDialog("Welcome! If you already have an account and would like to log in, please type '1'. If you would like to create an account, please type '2'.");
//convert string userOption to an int userNum
userNum = Integer.parseInt(userOption);
//validate input
if (userNum != 1 && userNum != 2)
{
JOptionPane.showInputDialog("Invalid input. Please type '1' to log in. To create a new account, type '2'.");
}
JOptionPane.showMessageDialog(null,"Good bye");
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.