Results 1 to 3 of 3
  1. #1
    biscuit is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Angry 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:

    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);
            }
    This is my console:

    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!

  2. #2
    biscuit is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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!

  3. #3
    biscuit is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Exception in thread "main"

    I got it working :-)

    What I did was to add the vInput.nextLine() to all fields that were receiving a string;

    Thanks!

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  2. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  3. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  4. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  5. Replies: 5
    Last Post: 06-22-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •