Java InputMismatchException loops with Scanner - java

first off this is one of my assignments for a class. I understand all the concepts my professor was trying to explain with this assignment but I am having a silly issue with it continuously looping, and cannot figure out how to stop it.
We were required to create our own Exception classes and use throw them at specific instances when a Time (with an hour, minute, and second) object is created. However, I'm not having a problem with my own exception classes, I am having an issue with the InputMismatchException continuously being executed.
This class reads a file through a Scanner object which has a few lines consisting of three integers on each line to represent a time in military format (ex. 20 15 33) is 8:15:33 P.M. but once there is an invalid token (ex. twenty 15 33 , where twenty is not an integer) it just won't stop the InputMismatchException catch block.
This is the code:
public class TimeTestNew
{
public static void main (String[] args)throws IllegalHourException,
IllegalMinuteException,
IllegalSecondException,
FileNotFoundException,
NumberFormatException
{
boolean fileFound = false;
while(!fileFound)
{
String input = JOptionPane.showInputDialog("Enter filename: " );
try
{
Scanner in = new Scanner(new File(input));
fileFound = true;
while(in.hasNextLine())
{
int hour = 0, minute = 0, second = 0;
try
{
hour = in.nextInt();
minute = in.nextInt();
second = in.nextInt();
Time theTime = new Time(hour,minute,second);
System.out.println(theTime);
}
catch(IllegalHourException e)
{
System.err.println("Sorry, \"" + e.value
+ "\" is an invalid hour.");
}
catch(IllegalMinuteException e)
{
System.err.println("Sorry, \"" + e.value
+ "\" is an invalid minute.");
}
catch(IllegalSecondException e)
{
System.err.println("Sorry, \"" + e.value
+ "\" is an invalid second.");
}
catch(NumberFormatException | InputMismatchException e)
{
System.err.println("Incorrect format used.");
// this is what keeps getting executed
}
}
in.close();
}
catch (FileNotFoundException e)
{
System.err.println("ERROR: \"" + input + "\" not found\n"
+ "Please enter a valid filename.");
}
}
}
}
And this is sample output:
12:12:12 P.M.
Sorry, "24" is an invalid hour.
1:02:03 A.M.
1:13:13 P.M.
Sorry, "90" is an invalid minute.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
Incorrect format used.
//...and this goes on forever
As you can see, it will loop that "Incorrect format used" inside the catch block for InputMismatchException over and over and over...I cannot figure out a way to get it to stop and continue reading the file.
Any help would be appreciated in explaining a solution, thanks!

The issue is that the while loop's condition isn't on whether the input is valid, but instead if there exists a next line in the file. You should check out the break statement if you don't have the ability to validate input before the while loop.

Related

handle Exceptions casued by input scanner

I'm trying to do encode/decode program and I'm encountering all kinds of Exceptions here!
problems that is popping up caused by multiple/single scanner/s:
InputMismatchException | NumberFormatException (ATTEMPT 2)
NoSuchElementException (ATTEMPT 3)
Before going through I would like to address that this is not a duplicate and I have looked up multiple problems on StackOverFlow of this kind and none helped me really much.
Similiar problems that I've looked at: link1 link2
Note that the wished end results are like the results of the first attempt but with somehow better cleaner exception handling and closed scanners.
FIRST ATTEMPT
Now this program gives me the desired results but its a bad programming to have two scanners and one of them ( input method scanner ) is never closed:
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int choice = 0;
do {
System.out.println("This program to encode or decode a byte array " +
"\n (o_O) Choices are: " +
"\n 1: Press 1 to enter the encode mode" +
"\n 2: Press 2 to enter the decode mode" +
"\n 3: Press 3 to Exit!");
try {
//it has to be parseInt because if you used sc.nextInt() the program will go nuts even with try catch.
choice=Integer.parseInt(sc.next());
//choice=sc.nextInt();
/*Question: why when i use this with the existing try catch i the program work for ever but when i use Integer.parseInt(sc.nextLine())
* the program would normally ask for another value?
*/
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("invalid type or format!");
} catch (NoSuchElementException e) {
System.out.println("no such");
//break; if i uncomment this the programm will work For Ever
}
switch(choice){
case 1 :
System.out.println("entering the encode mode!");
countAndEncode( input() );
break;
case 2 :
countAndDecode( input() );
break;
case 3 :
System.out.println("exiting...");
break;
default :
System.out.println("please enter a valid option and valid format!");
}
} while (choice!=3);
sc.close();
}
public static byte [] input() {
//arrayList because we dont know the size of the array its like StringBuilder
//ArrayList<Byte> inArray = new ArrayList<Byte>();
//according to StackOverflow using ArrayList to store bytes is inefficient
Scanner inScanner=new Scanner (System.in);
ByteArrayOutputStream inArray= new ByteArrayOutputStream();
System.out.println("enter a sequence of ints please! ");
System.out.println("non-int will terminate the input!");
while (inScanner.hasNext()) {
byte i;
try {
i = inScanner.nextByte();
inArray.write(i);
} catch (InputMismatchException e) {
System.out.println("input terminated!");
break;
}
}
//System.out.println(Arrays.toString(inArray.toByteArray()));
//inScanner.close();
return inArray.toByteArray();
}
OUTPUT OF FIRST ATTEMPT:
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
1
entering the encode mode!
enter a sequence of bytes please!
non-int will terminate the input!
1
1
3
e
input terminated!
[1, 1, 3]
the encoded list is [-1, 1, 2, 3]
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
At it goes forever without errors.
SECOND ATTEMPT
so what i did after one fellow of you guys sugessted to take a look at this problem link is this:
Now i didnt close the input scanner and i gave the input method a scanner as a parameter:
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int choice = 0;
do {
System.out.println("This is a program to encode or decode bytes based on RLE ALgorithm" +
"\n (o_O) Choices are: " +
"\n 1: Press 1 to enter the encode mode" +
"\n 2: Press 2 to enter the decode mode" +
"\n 3: Press 3 to Exit!");
try {
//it has to be parseInt because if you used sc.nextInt() the program will go nuts even with try catch.
choice=Integer.parseInt(sc.next());
//choice=sc.nextInt();
/*Question: why when i use this with the existing try catch i the program work for ever but when i use Integer.parseInt(sc.nextLine())
* the program would normally ask for another value?
*/
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("invalid type or format!");
} catch (NoSuchElementException e) {
System.out.println("no such");//TODO SOLVE IT PLEASE ITS DRIVING ME CRAZYYYYYYYYYYY!!!!!!!
break;
}
switch(choice){
case 1 :
System.out.println("entering the encode mode!");
countAndEncode( input(sc) );
break;
case 2 :
//countAndDecode( input(sc) );
break;
case 3 :
System.out.println("exiting...");
break;
default :
System.out.println("please enter a valid option and valid format!");
}
} while (choice!=3);
sc.close();
}
/**
* with this method user will be able to give the desired sequence of bytes.
* #return a byte array to be encoded.
*/
public static byte [] input(Scanner inScanner) {
//arrayList because we dont know the size of the array its like StringBuilder
//ArrayList<Byte> inArray = new ArrayList<Byte>();
//according to StackOverflow using ArrayList to store bytes is inefficient
//Scanner inScanner=new Scanner (System.in);
ByteArrayOutputStream inArray= new ByteArrayOutputStream();
System.out.println("enter a sequence of bytes please! ");
System.out.println("non-int will terminate the input!");
while (inScanner.hasNext()) {//TODO THIS MIGHT BE THE REASON FOR THE above "SUCH"
byte i;
try {
i = inScanner.nextByte();
inArray.write(i);
} catch (InputMismatchException e) {
System.out.println("input terminated!");
break;
}
}
System.out.println(Arrays.toString(inArray.toByteArray()));
//inScanner.close(); dont close it because it cant be re-opened
return inArray.toByteArray();
}
Doing so doesn't give me the desired results at all:
After choosing one to encode and receive the encoded bytes I will get stuck forever in the encoding mode and the InputMismatchException | NumberFormatException clause will get activated so I cant get a chance to select a new input!
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
1
entering the encode mode!
enter a sequence of bytes please!
non-int will terminate the input!
1
e
input terminated!
1
the encoded list is 1
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
invalid type or format!
entering the encode mode!
enter a sequence of bytes please!
non-int will terminate the input!
NOTES:
1.commenting sc.close() in main caused the exact same error as above..
2.that moving scanner above main and declaing it as a global static variable did the exact thing as the faild above results.
THIRD ATTEMPT
now i left both of the closed each scanner and this activated the NoSuchElementException in main Take a look:
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int choice = 0;
do {
System.out.println("This is a program to encode or decode bytes based on RLE ALgorithm" +
"\n (o_O) Choices are: " +
"\n 1: Press 1 to enter the encode mode" +
"\n 2: Press 2 to enter the decode mode" +
"\n 3: Press 3 to Exit!");
try {
//it has to be parseInt because if you used sc.nextInt() the program will go nuts even with try catch.
choice=Integer.parseInt(sc.next());
//choice=sc.nextInt();
/*Question: why when i use this with the existing try catch i the program work for ever but when i use Integer.parseInt(sc.nextLine())
* the program would normally ask for another value?
*/
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("invalid type or format!");
} catch (NoSuchElementException e) {
System.out.println("no such");//TODO SOLVE IT PLEASE ITS DRIVING ME CRAZYYYYYYYYYYY!!!!!!!
break;
}
switch(choice){
case 1 :
System.out.println("entering the encode mode!");
countAndEncode( input() );
break;
case 2 :
//countAndDecode( input() );
break;
case 3 :
System.out.println("exiting...");
break;
default :
System.out.println("please enter a valid option and valid format!");
}
} while (choice!=3);
sc.close();
}
/**
* with this method user will be able to give the desired sequence of bytes.
* #return a byte array to be encoded.
* #throws IOException
*/
public static byte [] input() {
//arrayList because we dont know the size of the array its like StringBuilder
//ArrayList<Byte> inArray = new ArrayList<Byte>();
//according to StackOverflow using ArrayList to store bytes is inefficient
Scanner inScanner=new Scanner (System.in);
ByteArrayOutputStream inArray= new ByteArrayOutputStream();
System.out.println("enter a sequence of bytes please! ");
System.out.println("non-int will terminate the input!");
while (inScanner.hasNext()) {//TODO THIS MIGHT BE THE REASON FOR THE above "SUCH"
byte i;
try {
i = inScanner.nextByte();
inArray.write(i);
} catch (InputMismatchException e) {
System.out.println("input terminated!");
break;
}
}
System.out.println(Arrays.toString(inArray.toByteArray()));
inScanner.close();
return inArray.toByteArray();
}
in this attempt i , at least, might know what is causing the NoSuchElementException to jump up and i think its because closing one scanner will close the input stream for the whole code.(correct me if im wrong!)
OUTPUT FOR THE THIRD ATTEMPT IS:
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
1
entering the encode mode!
enter a sequence of bytes please!
non-int will terminate the input!
-1
-1
e
input terminated!
[-1, -1]
the encoded list is [-1, -1, -1, -1]
This is a program to encode or decode bytes based on RLE ALgorithm
(o_O) Choices are:
1: Press 1 to enter the encode mode
2: Press 2 to enter the decode mode
3: Press 3 to Exit!
no such
SOLUTION TO DISCUSS BY #Villat
First of all big thanks to you man for helping and investing time and effort.
Now, i have small question regarding these lines:
if(sc.hasNextInt()) choice=sc.nextInt();
else {
sc.next();
continue;
}
error = false;
So let me see if i got this right, those lines play a role as a precaution,and please correct me if im wrong!, to prevent the Exception from popping up right.
So wouldnt it be enough to write the following ditching the try-catch blocks because NoSuchElementException has no chance to emerge and the InputMismatchException is being treated and prevented by the else block:
while (error){
if(sc.hasNextInt()) choice=sc.nextInt();
else {
sc.next();
continue;
}
error = false;
}
Just for training purpouses if i would like to Handle this error by try-catch block would you consider it clean and immune to Exceptions if i wrote it like this: (ditching the NumberFormatException)
-so Demonstrating the Handle variant of your answer would it be like this right?
while (error){
try {
choice=sc.nextInt();
error = false;
} catch (InputMismatchException /*| NumberFormatException*/ e) {
error = false;
//System.out.println("invalid type or format!");
sc.next();
continue;
}
}
I made a few changes to your code (and removed comments to make it more readable). Basically, I'm only using one Scanner right now, and I'm not moving forward into the options until a sc.nextInt() appears.
public static void main(String[] args){
Scanner sc=new Scanner (System.in);
int choice = 0;
do {
System.out.println("This is a program to encode or decode bytes based on RLE ALgorithm" +
"\n (o_O) Choices are: " +
"\n 1: Press 1 to enter the encode mode" +
"\n 2: Press 2 to enter the decode mode" +
"\n 3: Press 3 to Exit!");
boolean error = true;
while (error){
try {
if(sc.hasNextInt()) choice=sc.nextInt();
else {
sc.next();
continue;
}
error = false;
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("invalid type or format!");
} catch (NoSuchElementException e) {
System.out.println("no such");
}
}
switch(choice){
case 1 :
System.out.println("entering the encode mode!");
System.out.println(input(sc));
break;
case 2 :
//countAndDecode(input(sc));
break;
case 3 :
System.out.println("exiting...");
break;
default :
System.out.println("please enter a valid option and valid format!");
}
} while (choice!=3);
sc.close();
}
Input method:
public static byte [] input(Scanner sc) {
ByteArrayOutputStream inArray= new ByteArrayOutputStream();
System.out.println("enter a sequence of bytes please! ");
System.out.println("non-int will terminate the input!");
while (sc.hasNext()) {
byte i;
try {
i = sc.nextByte();
inArray.write(i);
} catch (InputMismatchException e) {
System.out.println("input terminated!");
break;
}
}
System.out.println(Arrays.toString(inArray.toByteArray()));
return inArray.toByteArray();
}

No Such Element Exception with Java.util.scanner for nextInt()

Trying to do some Command Line Arguments for a program. Next int is clearly present but Scanner throws NoSuchElement exception. With or without new line/whitespace skip, trim() on string before it, etc.
try {
String s = String.valueOf(scan.next().charAt(0));
String t = String.valueOf(scan.next().charAt(0));
if ((s+t).trim() != "-s") {
throw new IOException ("Size must be allocated with ā€œ-sā€.");
}
System.out.println("Got ''-s'' successfully.");
} catch (IOException SizeFormatIncorrect) {
}
try {
//scan.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); - with or
//without, gives same problem
size = scan.nextInt();
System.out.println("Got " + size);
} catch (NumberFormatException NotInt)
{ }
Input:
-s 200 or -s 2
OR
-s\n200 or -s\n2
Output:
Enter variables to test call:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at HelloWorld.main(HelloWorld.java:28)
//(Line 28 is size = scan.nextInt())
//If I add
if (!scan.hasNextInt()){
System.out.println("We have :" + scan.nextLine().getBytes());
}
I get output:
We have :[B#42a57993
So whoever wants to tell me and everyone else reading how to cast whatever it is Scanner thinks it's reading in to an Int. (Integer.parseInt() wouldn't work)
I properly allocated scan with Scanner scan = new Scanner(System.in)
Then Scanner reads from System.in (standard input). Not the command line. You could concatenate your arguments into a String and then use that as a source for your Scanner. Like,
Scanner scan = new Scanner(String.join(" ", args));
You will also need to change (at least)
(s+t).trim() != "-s"
because that is not a good way to compare String(s). You need something like
!(s+t).trim().equals("-s")

How to reprompt for invalid Boolean input (java)

When I run the program if I type something other than "true" or "false" it throws a InputMismatchException.
do {
System.out.print("Do passengers have an individual tv screen?"
+ "(true OR false): ");
hasVideo = keyboard.nextBoolean();
bus.setIndividualVideo(hasVideo);
} while (!(hasVideo == true) && !(hasVideo == false));
Catch the error and treat it as a invalid response...
try {
System.out.print("Do passengers have an individual tv screen?"
+ "(true OR false): ");
hasVideo = keyboard.nextBoolean();
} catch (InputMismatchException exp) {
System.err.println("Please, enter only true or false");
}
Take a look at The try Block for more details
Aha, time to learn about Exception handling! Any of the Exception types that you see when java crashes can in fact be caught inside the program with a try-catch block.
try {
// code that might throw exceptions 1
// code that might throw exceptions 2
} catch (Exception e) {
// do something to fix the error
}
If any code in the try{ } part does throw an Exception then it will immediately skip to the catch( ) { } part, skipping any other statements in the try{ }.
Your code with a try-catch would look like:
boolean loopAgain = false;
do {
try {
System.out.print("Do passengers have an individual tv screen?"
+ "(true OR false): ");
hasVideo = keyboard.nextBoolean();
bus.setIndividualVideo(hasVideo);
loopAgain = false;
} catch (InputMismatchException e) {
System.err.println("Please, enter only true or false");
loopAgain = true;
}
} while (loopAgain);
Edit: I borrowed the println("Please, enter only true or false"); from #MadProgrammer's answer.
You have to prompt the user to enter a Boolean value. Because nextBoolean() can throw an exception, the best thing to do is to put that code inside a try/catch. The catch block code is only executed if anything other than true or false is entered. You can add a while() or do/while() loop to keep telling the user to try again. However, the most important thing to do in the catch block is to flush the input stream. Remember, even though there was an exception, the stream still contains stuff in it. It has to be properly consumed before using again. The code below should do exactly what you are looking for:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Boolean answer = null;
do
{
System.out.println("Enter either true or false");
try
{
answer = input.nextBoolean();
}
catch(InputMismatchException e)
{
System.out.println("ERROR: The input provided is not a valid boolean value. Try again...");
input.next(); // flush the stream
}
} while(answer == null);
input.close();
}

hasNextInt() From Scanner behaving weirdly

I have a very simple loop that waits for a number (int) and as long as that number is not exitOption it does not leave the loop, however I get an unexpected error, and I don't know what's causing it.
Edit
Adding another snippet so you can compile
public static void main(String[] args) throws FileNotFoundException,
SecurityException,
IOException,
ClassNotFoundException {
while (controller.selectOptionMM());
/Edit
public boolean selectOptionMM() throws SecurityException,
FileNotFoundException,
IOException {
int cmd = ui.getExitOption();
ui.mainMenu();
cmd = utils.readInteger(">>> "); // this is my problem, right here
// code in next snippet
while (cmd <1 || cmd > ui.getExitOption()) {
System.out.println("Invalid command!");
cmd = utils.readInteger(">>> ");
}
switch (cmd) {
case 1:
case 2:
case 3:
case 4: this.repository.close();
return true;
case 5: return false;
}
return false;
}
Here is what fails:
public int readInteger(String cmdPrompt) {
int cmd = 0;
Scanner input = new Scanner(System.in);
System.out.printf(cmdPrompt);
try {
if (input.hasNextInt())
cmd = input.nextInt(); // first time it works
// Second time it does not allow me to input anything
// catches InputMissmatchException, does not print message
// for said catch
// infinitely prints "Invalid command" from previous snippet
} catch (InputMismatchException ime) {
System.out.println("InputMismatchException: " + ime);
} catch (NoSuchElementException nsee) {
System.out.println("NoSuchElementException: " + nsee);
} catch (IllegalStateException ise) {
} finally {
input.close(); // not sure if I should test with if (input != null) THEN close
}
return cmd;
}
First time I pass trough, it reads the number no problem. Now if the number is not 5 (in this case exitOption), it passes again trough readInteger(String cmdPrompt) except this time it jumps to catch (InputMismatchException ime) (debug) except it does not print that message and just jumps to Error, input must be number and Invalid command.
Is something stuck in my input buffer, can I flush it, why is it (input buffer) stuck (with random data)???
I'll try debugging again and see what's stuck in my input buffer, if I can figure out how to see that.
The problem is in the call to input.close() - this causes the underlying input stream to be closed. When the input stream being closed is System.in, bad things happen (namely, you can't read from stdin any more). You should be OK just eliminating this line.
input.hasNextInt()
This line throws the exception if there is no Integer, so instead of it going to else block it forward to catch block. It will never go to else block if exception get caught.

Java accepting only numbers from user with Scanner

I am trying to understand how to only accept numbers from the user, and I have attempted to do so using try catch blocks, but I would still get errors with it.
Scanner scan = new Scanner(System.in);
boolean bidding;
int startbid;
int bid;
bidding = true;
System.out.println("Alright folks, who wants this unit?" +
"\nHow much. How much. How much money where?" );
startbid = scan.nextInt();
try{
while(bidding){
System.out.println("$" + startbid + "! Whose going to bid higher?");
startbid =+ scan.nextInt();
}
}catch(NumberFormatException nfe){
System.out.println("Please enter a bid");
}
I am trying to understand why it is not working.
I tested it out by inputting a into the console, and I would receive an error instead of the hopeful "Please enter a bid" resolution.
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 Auction.test.main(test.java:25)
Try catching the type of exception thrown rather than NumberFormatException (InputMismatchException).
The message is pretty clear: Scanner.nextInt() throws an InputMismatchException, but your code catches NumberFormatException. Catch the appropriate exception type.
While using Scanner.nextInt(), it causes some problems. When you use Scanner.nextInt(), it does not consume the new line (or other delimiter) itself so the next token returned will typically be an empty string. Thus, you need to follow it with a Scanner.nextLine(). You can discard the result.
It's for this reason that I suggest always using nextLine (or BufferedReader.readLine()) and doing the parsing after using Integer.parseInt(). Your code should be as follows.
Scanner scan = new Scanner(System.in);
boolean bidding;
int startbid;
int bid;
bidding = true;
System.out.print("Alright folks, who wants this unit?" +
"\nHow much. How much. How much money where?" );
try
{
startbid = Integer.parseInt(scan.nextLine());
while(bidding)
{
System.out.println("$" + startbid + "! Whose going to bid higher?");
startbid =+ Integer.parseInt(scan.nextLine());
}
}
catch(NumberFormatException nfe)
{
System.out.println("Please enter a bid");
}

Categories