Error if someone enters the wrong data type
Hello,
I have a program that is currently accepting 4 different integers from a user. However, I need to put limit to the digit value the user can input, also if the user inputs a character, or a string, in other words, the wrong data type, the program shows an errors and asks the user to try again.
The first integer would be between 1 and 40
The second integer would be between 1 and 20
The third integer can be between 1 and the highest value of the first integer
The fourth integer can be between 1 and the highest value of the second integer
I have been trying to use the "input.hasnextInt" but I'm honestly lost.
Here is what my input fields look like:
Quote:
Scanner input = new Scanner(System.in);
System.out.print("The X co-ordinate: "); // the x-axis location on the graph
int x = input.nextInt();
System.out.print("The Y co-ordinate: "); // the y-axis location on the graph
int y = input.nextInt();
System.out.print("Please enter a width for your rectangle: "); // the width of the rectangle to be plotted
int width = input.nextInt();
System.out.print("Now enter a height for your rectangle: "); // the height of the rectangle to be plotted
int height = input.nextInt();
Can anyone guide me in the right direction to start this? Should I use the input.hasnextInt within java, and if so, how is it implemented?
I keep getting errors!