This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I keep getting this error when trying to run my program: Exception in thread "main" java.util.InputMismatchException and I know it's because of my scanner. I have tried messing around with the scanner for a couple hours now and I can't find the solution....so I come to the internet to hopefully get help with this. My code consists of three classes but I suspect the problem is in one of the classes which does all of the scanning and retrieving of information from a .csv file.
You get an InputMismatchException (IME) whenever a Scanner cannot convert the String it has to its return value. For example, if myScanner.nextInt() encounters text like "123.45", it will throw an IME.
Two likely causes:
1: Your keyboard input isn't in the correct format Try putting the reading inside a loop like this, though it would be better to write a utility class and do all your reading via that utility class.
2: You are reading a blank end of line That problem only occurs when you use the nextLine() method; the documentation tells you what nextLine() does, but it doesn't warn you that nextLine() doesn't do what you think it does. Look at this old post which describes that problem.
Joined: Oct 13, 2005
Why are you using \n as a delimiter? You should check what the default delimiter is, and also what line end on Windows® is. You will find that using \n might lead to errors on Windows files (or old Mac files).