Reputation: 11
I wrote a program to determine a prime number and am required to use 'Q' as the quit function to get out of the program. Below is the code i have written. I am wondering how I would modify method of quitting the program to make it exit correctly.
import java.util.Scanner;
public class Practice
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
int number, i=2;
String quit;
boolean prime = true;
System.out.println("Please enter a number to determine if it is prime, to quit please enter Q.");
number = keyboard.nextInt();
quit = keyboard.nextLine();
for (i=0; i<number;i++)
{
if ((number%2==0) || (number==1) || (number%3==0))
{
prime = false;
}
}
if (prime==false)
{
System.out.println(number+" is not a prime number.");
}
else if(prime==true)
System.out.println(number+" is a prime number.");
if (quit.charAt(0)=='Q')
{ System.exit(0);}
and my output upon entering 'Q' is:
----jGRASP exec: java Practice
Please enter a number to determine if it is prime, to quit please enter Q.
Q
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Practice.main(Practice.java:15)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
}
}
Upvotes: 0
Views: 2417
Reputation: 6192
Try this instead:
String input;
int number = 1;
int i=2;
Then do this:
System.out.println("Please enter a number to determine if it is prime, "
+ "\nto quit, please enter Q.");
input = keyboard.nextLine();
if (input.charAt(0)=='Q')
{
System.exit(0);
}
else
{
try
{
number = Integer.parseInt(input);
}
catch(NumberFormatException nfe)
{
System.out.println("Please enter a number or Q!");
}
}
Upvotes: 0
Reputation: 41
You should read in the numbers as Strings with keyboard.nextLine(); and check first if the it contains 'Q'. If it does, quit, else do, number = Integer.parseInt(yourVariableHere); and go from there
Upvotes: 0
Reputation: 40315
You have:
number = keyboard.nextInt();
quit = keyboard.nextLine();
Think for a moment about what actually happens here. 'Q' is not a number and so nextInt()
, as documented, throws an InputMismatchException
. Your use of nextLine()
doesn't make much sense following that.
You have a couple of options:
Scanner.hasNextInt()
to determine if the next token truly is a well-formed integer. If not, read it as a string and check it.Scanner.next()
always, and check if it is "Q" before parsing it with Integer.parseInt()
(being prepared to handle errors there as well).The Scanner.next*
functions don't implicitly skip invalid values. The Scanner
is only doing what you tell it to do, and if you tell it to grab an integer but the input is not an integer, it rightfully complains. It is up to you to use the Scanner
appropriately and check your possible input cases.
Upvotes: 0
Reputation: 29
When the input through the console is given as "Q", the value of type String is getting assigned to a variable of type int, which is incorrect.
number = keyboard.nextInt();
The type of in the input needs to be checked, before it can be assigned to a int variable.
Upvotes: 1