Results 1 to 3 of 3
Thread: Exception in thread "main"
- 02-13-2013, 06:02 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Exception in thread "main"
Hi guys,
In my main method I read info from user using Scanner and I get an error when I read the next value. Code is as follows:
This is my console:Java Code:String isbn, title, author; String answer; int copyrightYear; double price; int quantity; //create new MyLibrary MyLibrary lib = new MyLibrary("Library"); //Linked list of books vInput = new Scanner(System.in); //Ask user to add a new book System.out.printf("Add new book? Y/N "); answer = vInput.next().toUpperCase(); while (answer.equals("Y")){ //Prompt user for book info System.out.printf("ISBN..........: "); isbn = vInput.next(); System.out.printf("Title.........: "); title = vInput.next(); System.out.printf("Author........: "); author = vInput.next(); //The exception happens here System.out.printf("Copyright Year: "); copyrightYear = vInput.nextInt(); System.out.printf("Price.........: "); price = vInput.nextDouble(); System.out.printf("Quantity......: "); quantity = vInput.nextInt(); //Add Book to library lib.addBook(isbn, title, author, copyrightYear, price, quantity); }
Add new book? Y/N Y
ISBN..........: 978-1-4419-8833-1
Title.........: Özsu
Author........: Principles of Distributed Database Systems
Copyright Year: Exception in thread "main" java.util.InputMismatchException
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 org.totalbeginner.tutorial.MyLibrary.main(MyLibrar y.java:164)
Any ideas? It seems like it doesn't like the change from String to int... but I googled around and saw that it should not happen...
I don't know what the heck is wrong...
Thanks!
- 02-13-2013, 06:30 AM #2
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Re: Exception in thread "main"
I noticed that it seems like if I add a string with a space on it it considers that the next is the word that comes after the blank space then it is what causes it to fail... how do I read the whole sentence?
Thanks!
- 02-13-2013, 06:45 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Error: "Exception in thread "main" java.util.NoSuchElementException"
By Mattiscool in forum New To JavaReplies: 1Last Post: 11-02-2012, 11:38 PM -
"Exception in thread "main" java.lang.NoSuchMethodError: main"
By isnkumar in forum New To JavaReplies: 2Last Post: 06-20-2012, 12:18 AM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks