Reputation: 21
Quick question. Still fairly new to Java and my test class is giving me this error.
Please enter length of tail: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at LeftArrow.drawHere(LeftArrow.java:18)
at ArrowTest.main(ArrowTest.java:75)
Here is the code for the test class
public class ArrowTest
{
public static void main(String args[])
{
RightArrow right = new RightArrow();
LeftArrow left = new LeftArrow();
System.out.println("RightArrow Class: Calling drawHere()");
right.drawHere();
System.out.println("LeftArrow Class: Calling drawHere()");
left.drawHere(); //Error pointing to this line
// (at ArrowTest.main(ArrowTest.java:75))
}
}
And here is the code for the RightArrow and LeftArrow classes, just the relevant code. I commented the lines that the error was referring to.
public class RightArrow extends ShapeBase
{
public void drawHere()
{
int lengthTail, widthHead;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter length of tail: ");
lengthTail = kb.nextInt();
System.out.print("Please enter an odd numbered width of arrowhead: ");
widthHead = kb.nextInt();
}
public class LeftArrow extends ShapeBase
{
public void drawHere()
{
int lengthTail, widthHead;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter length of tail: ");
lengthTail = kb.nextInt(); //Error pointing to this line at LeftArrow.drawHere(LeftArrow.java:18)
System.out.print("Please enter an odd numbered width of arrowhead: ");
widthHead = kb.nextInt();
}
I commented out the right.drawHere() in the test class and it seemed to work okay. So I am fairly convinced it is because I am calling the same method from two classes derived from the same abstract class. Is there a way that I can fix this? Thanks for your help!
EDIT: I found that if I don't close the kb from the first arrow class called upon it does not throw this error. I can only assume its because of me closing System.in which is why it's causing a problem. Can anyone explain to me why making another instance of Scanner(System.in) doesn't just re-"open" System.in?
Upvotes: 0
Views: 118
Reputation: 2023
The problem is with Scanner object where while you calling right.drawHere()
there you initialized the Scanner kb with System.in
this will buffer through all data available in System.in
left it empty for future use therefore it is throwing NoSuchElementException in left.drawHere()
.
Solution:
Make Scanner
object kb
as static and accessible for both LeftArrow
and RightArrow
Upvotes: 0
Reputation: 4176
Before calling the nextInt
method, you can use the hasNextInt()
method on the scanner object to check if there is a value to get, which can be used to avoid exception. Try this:
if (kb.hasNextInt()){
lengthTail = kb.nextInt();
}
In your case, I think you are not providing enough inputs, which could lead to NoSuchInputException
.
Have a look at the Scanner class doc for more information.
Upvotes: 1