Reputation: 15
I have written some code to check if the user has 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 combine the code where I can have it identify whether the user has entered 1-5 or A, S, D, M?
How do I edit the code below so the user can enter either an Integer or a character? Do I have to write a snippet of code underneath the loop for it to identify that a user did not enter 1-5 but did enter A, S, D, or M, as in break out of the loop? Or is it a separate loop all together. I am 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;
}
}
Upvotes: 3
Views: 471
Reputation: 35481
As @MarsAtomic mentioned, first thing you should change your input to String
instead of an int
so you can easily handle both characters and digits.
Change:
int inputInt;
To:
String input;
Then change:
inputInt = readInput.nextInt();
To:
input = readInput.next();
To accommodate reading String
instead of int
.
Now you reach at 2 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 don't need to use the try/catch
statement and can print your errors in else
blocks.
Furthermore, you should have your method return a char
or a String
instead of an int
so you can return both 1-5
or A,S,D,M
. I will assume you want to return a char
. If you want to return a String
instead, you can return input
instead of return val
in the code bellow.
NOTE: The code bellow can be simplified and shortened, I just added variables and comments in an attempt to make each step clear to what is being read or converted. You can look at @mikeyaworski's answer for a more concise way of doing this.
Here is how your code could 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);
}
Upvotes: 1
Reputation: 13483
I suggest instead of using an int
input, just use a String
input and convert it to an integer when you need to. You can use Integer.parseInt(String)
to convert a String
to an int
.
So when you check if the input is valid, you need to check if the input is equal to "A"
, "S"
, "M"
or "D"
, or any values from 1-5 when it is converted to an int
.
So to check if it's one of the characters, you could do this:
if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D"))
And then to test if it's an int
of value 1 through 5, you could do this:
if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5)
Just parse the input to an int
and then check the range as you already have done.
The return type of this method will be String
now, instead of int
. If you need it to be an int
for whatever reason, you can just parse the value to an int
and then return that instead. But I just returned it as a String
.
The last thing I changed was the catch
block. Now, instead of an InputMismatchException
(because they can enter String
s now, I changed it to NumberFormatException
, which would happen if a String
that could not be converted to an int
was attempted to be. For example, Integer.parseInt("hello")
will throw a NumberFomatException
because "hello"
can not be represented as an integer. But, Integer.parseInt("1")
would be fine and would return 1
.
Note that you should test the String
equivalence first so that you don't go into your block
before you have a chance to test all conditions you need to.
The method would 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;
}
Upvotes: 1
Reputation: 36
If your input can be both characters and letters, why not change to looking for a char or String? Then, you can look for "1" or "A" without any trouble.
Upvotes: 0