Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote