aspose file tools*
The moose likes Beginning Java and the fly likes fatal logic scanner error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "fatal logic scanner error" Watch "fatal logic scanner error" New topic
Author

fatal logic scanner error

Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

Hi,

When executing this code I get a Scanner error whch I thnk is syaing a input integer type is expected but not seen, although the user inout is integer :

Error:
Enter product (1 to 5) purchased.
NB: When finished select <crtl> d if using a Mac and <crtl> z for windows OS:20
1
Enter the amount of product sold: 2


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 Sales.calculateSales(Sales.java:33)
at SalaryText.main(SalaryText.java:11)
[b]
[/b]



Cheers
Ciaran


I love deadlines, I love the whooshing sound they make as they fly by...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And what is the input which causes this Exception? I tried your code and it ran without exceptions.
Your lines are too long, particularly those comments, some of which are unnecessary. I have broken some into smaller components. Never use tabs for indenting; use spaces. Avoid all those empty lines. Only use \n if you have been told you require LF. After printf you should use %n
Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

The exception occurs after the user inputs a integer
representing the Quanity when prompted "Enter amount of product sold" at line 33.

Wat does 'LF' stand for ?

The program exectues ok for you?Still wont execute when i try to run it in terminal window.

C
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
LF = line feed.
Correct the \n to %n, break those long lines into shorter lines, and put a print statement for "please enter product code". Then please post your new code, and we shall see what happens then.
Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

OK, so I have tried changing \n to %n and got same error.
I then removed the \n altogether and still error persists.

I think the problem lies with the else if statement on line 66 as System.out.print() checks I have put in stop at this point.

When I run in termonal window I see this (after I have inout 1 for product and 1 for quantity):

Ciarans-MacBook-Pro:Module 5 callanmooneys$ java SalaryText
Enter product (1 to 5) purchased. NB: When finished select <crtl> d if using a Mac and <crtl> z for windows OS:1
Enter the amount of product sold:
1
>>>1<<<
>>>1<<<
>>>case11<<<>>>1<<<>>>check2.98<<<>>>post switch break check2.98<<<



1


The program then stops as if awaiting input but doesnt respond when I press return, if I "^" in term window and then hit return I get the error:

^[[A
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 Sales.calculateSales(Sales.java:30)
at SalaryText.main(SalaryText.java:11)

[/b]



So im out of ideas.

code is:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
If you are going to use a switch statement, use a switch statement. Don’t try combining it with if‑elses. You can get the error printed from the switch; that is what the default keyword is for, and you don’t need the if‑else. And always use break; unless you intend fall‑through.
Apart from the fact you haven’t written a main method, I can’t get that Exception. You must be entering something which isn’t an int. Do some searching of my posts and see how to create a utility class which returns your ints and never throws an Exception. If you don’t find it within 10 minutes, I shall have a look.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I did a search and found one of my old posts which has the answer in for you. I won’t tell you where it is, but it might just possibly be in this thread. There are also potential problems with nextLine causing the Scanner to get out of phase with the input. If you
find my post, there is a link to another discussion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And you ought to put the "please enter next product number" print statement inside the loop.
Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

Got it

The program was running excatly as Id asked.....at the start of the while loop it was waiting for input (input.hasNext), this caused confusion as there was no prompt.
So when I pressed the ^ key im assuming the Scanner wasnt expecting this and gave the error.

Ive changed the while condition to (value!=0) and it works fine.


A case of smart computer, stupid human I suppose.

Thanks for the help ive learnt allot.

ps is there a method in java that returns the input value type, like python type(value) will tell you wether its primitive type or reference


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Sorted out Well done.
When I saw no output prompt, I pushed enter a few times, and then a number between 1 and 5. So nextInt() found it eventually.
I don’t think * counts as an int, but the technique in the utility class in the other post (which was originally suggested by Rob Spoor) would have had an output like “That wasn’t a number. Try again:” instead of the Exception.
 
 
subject: fatal logic scanner error