Multiple variable for if else statement
I wrote some code to check if the user entered a number between 1 and 5, and now I would also like my code to allow the user to enter the letters A, S, D, or M.
Is there a way to concatenate the code where I can tell if the user is entered 1-5 or A, S, D, M?
How do I change the code below so that the user can enter an Integer or a character? Should I write a piece of code below the loop so that it identifies that the user is not typing 1-5 but typing A, S, D, or M, just like when exiting the loop? Or is it a separate loop all together. I'm so confused!
import java.util.InputMismatchException;
import java.util.Scanner;
public class Selection {
Scanner readInput = new Scanner(System.in);
int selectionOne() {
int inputInt;
do { //do loop will continue to run until user enters correct response
System.out.print("Please enter a number between 1 and 5, A for Addition, S for subtraction, M for multiplication, or D for division: ");
try {
inputInt = readInput.nextInt(); //user will enter a response
if (inputInt >= 1 && inputInt <=5) {
System.out.print("Thank you");
break; //user entered a number between 1 and 5
} else {
System.out.println("Sorry, you have not entered the correct number, please try again.");
}
continue;
}
catch (final InputMismatchException e) {
System.out.println("You have entered an invalid choice. Try again.");
readInput.nextLine(); // discard non-int input
continue; // loop will continue until correct answer is found
}
} while (true);
return inputInt;
}
}
source to share
I suggest, instead of typing, int
just use the input String
and convert it to an integer when you need it. You can use Integer.parseInt(String)
to convert String
to int
.
So when you check if an input is valid, you need to check if the input "A"
is "S"
, "M"
or "D"
, or any values ββfrom 1 to 5 when converting it to int
.
So, to check if it is one of the characters, you can do this:
if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D"))
And then to check if the value int
is between 1 and 5, you can do this:
if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5)
Just parse the input int
and then check the range as you already did.
The return type of this method will String
now be instead of int
. If you need it to be there int
for any reason, you can simply parse the value before int
and then return that instead. But I just returned it as String
.
The last thing I changed is the block catch
. Now instead of InputMismatchException
(because now they can enter String
, I changed it to NumberFormatException
) which would happen if I tried to be String
, which could not be converted to int
. For example, it Integer.parseInt("hello")
will call NumberFomatException
because it "hello"
cannot be represented as an integer. But it Integer.parseInt("1")
will be fine and will return 1
.
Note that you need to check for equivalence first String
so that you are not logged into block
yours before you have a chance to check all the conditions that you need.
The method will look like this:
String selectionOne() {
String input;
do { //do loop will continue to run until user enters correct response
System.out.print("Please enter a number between 1 and 5, A for Addition, S for subtraction, M for multiplication, or D for division: ");
try {
input = readInput.nextLine(); //user will enter a response
if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D")) {
System.out.println("Thank you");
break; //user entered a character of A, S, M, or D
} else if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5) {
System.out.println("Thank you");
break; //user entered a number between 1 and 5
} else {
System.out.println("Sorry, you have not entered the correct number, please try again.");
}
continue;
}
catch (final NumberFormatException e) {
System.out.println("You have entered an invalid choice. Try again.");
continue; // loop will continue until correct answer is found
}
} while (true);
return input;
}
source to share
As @MarsAtomic mentioned, you must first change your input to String
instead int
so that you can handle both characters and numbers easily.
Edit:
int inputInt;
To:
String input;
Then change:
inputInt = readInput.nextInt();
To:
input = readInput.next();
To perform reading String
instead of int
.
You now achieve two main cases (and 2 subcases):
1) input is a single character
a) input is a single digit from 1-5
b) input is a single character from the set ('A', 'S', 'D', 'M')
2) input is an error value
Also, since you are not calling Scanner.nextInt
, you do not need to use the operator try/catch
and can print your errors in blocks else
.
Also, you must return your method char
or String
instead int
, so that you can return both 1-5
and A,S,D,M
. Let's say you want to return char
. If you want to return String
instead, you can return input
instead return val
in the code below.
NOTE. ... The code below could be simplified and shortened, I just added variables and comments, trying to make each step clear for what is being read or converted. You can look at @ mikeyaworski's answer for a more concise way to do this.
This is what your code would look like:
char selectionOne() {
String input;
do {
input = readInput.next();
// check if input is a single character
if(input.length() == 1) {
char val = input.charAt(0);
// check if input is a single digit from 1-5
if(Character.isDigit(val)) {
int digit = Integer.parseInt(input);
if (digit >= 1 && digit <=5) {
System.out.print("Thank you");
return val; // no need to break, can return the correct digit right here
} else {
System.out.println("Sorry, you have not entered the correct number, please try again.");
}
} else {
// check if input is in our valid set of characters
if(val == 'A' || val == 'S' || val == 'M' || val == 'D') {
System.out.print("Thank you");
return val; // return the correct character
} else {
System.out.println("Sorry, you have not entered the correct character, please try again.");
}
}
} else {
System.out.println("Sorry, you have not entered the correct input format, please try again.");
}
} while(true);
}
source to share