2013-10-31 30 views
0

所以我的程序運行良好,但是當我在項目用空格輸入它拋出一個java.util.InputMismatchException。所以我改變了所有的kb.next()來kb.nextLine(),但與遇到的問題IM是例如:附加部分,只要它打印請輸入你想添加標題。,它沒有讓我輸入任何東西,它只是跳過到下一行並打印請輸入有你想添加的項目的價值。Java的掃描儀串用空格

System.out.println("Please enter the title that you would like to add."); 
        title = kb.next(); 

System.out.println("Please enter the want value of the item that you would like to add."); 
        wantValue = kb.nextInt(); 

System.out.println("Please enter the have value of the item that you would like to add."); 
        haveValue = kb.nextInt(); 

package assignment3; 

import java.util.Scanner; 
public class Question1 
{ 
    static SortedList inventory; 

    //main 
    public static void main(String[] args) 
    { 
     inventory = new SortedList(); 
     @SuppressWarnings("resource") 
     Scanner kb = new Scanner(System.in); 
     String command, title; 
     int wantValue, haveValue; 

     do 
     { 
      System.out.println("\n    MAIN MENU"); 
      System.out.println("H. Provide a summary of available commands"); 
      System.out.println("L. List the entire inventory."); 
      System.out.println("I. Display Inventory information for a specific title."); 
      System.out.println("A. Add a new title to Inventory."); 
      System.out.println("M. Modify the want value of a specific title."); 
      System.out.println("S. Sell an item"); 
      System.out.println("O. Write a purchase order for additional DVD's"); 
      System.out.println("R. Write a return order."); 
      System.out.println("Q. Quit the program."); 
      command = kb.next(); 

      //If the user need a summary of available commands. 
      if(command.equalsIgnoreCase("H")) 
      { 
       System.out.println("H (help), provides a summary of available commands."); 
       System.out.println("L (list), list the entire inventory."); 
       System.out.println("I<title> (inquire), Display theinventory information for a specific title."); 
       System.out.println("A<title> (add), add a new title to the existing inventory."); 
       System.out.println("M<title> (modify), modify the want value of a specific title."); 
       System.out.println("S<title> (sell), sell an item, decrease have value of that item by 1."); 
       System.out.println("O (order), write an order for additional items so have value = want value."); 
       System.out.println("R (return), return extras so have value = want value."); 
       System.out.println("Q (quit), quit the program."); 
      } 

      //If the user want the inventory info on a specific title 
      else if(command.equalsIgnoreCase("I")) 
      { 
       System.out.println("Please enter the title that you would like to inquire."); 
       title = kb.next(); 
       //Search through the inventory 
       for(int i =1; i<=inventory.size();i++) 
       { 
        StockItem item = (StockItem)inventory.get(i); 
        //if a matching title is found 
        if((item.getTitle()).equals(title)) 
        { 
         //print out the item 
         String s= item.toString(); 
         System.out.println(s); 
        } 
       } 
      } 

      //If the user want a list the entire inventory. 
      else if(command.equalsIgnoreCase("L")) 
      { 
       //Search through the inventory 
       for(int i =1; i <=inventory.size(); i++) 
       { 
        StockItem item = (StockItem)inventory.get(i); 
        //print out the item 
        String s= item.toString(); 
        System.out.println(s); 
       } 
      } 

      //If the user want to add a new item to the inventory 
      else if(command.equalsIgnoreCase("A")) 
      { 
       System.out.println("Please enter the title that you would like to add."); 
       title = kb.next(); 

       System.out.println("Please enter the want value of the item that you would like to add."); 
       wantValue = kb.nextInt(); 

       System.out.println("Please enter the have value of the item that you would like to add."); 
       haveValue = kb.nextInt(); 

       //Add the new item to the inventory 
       StockItem NewItem = new StockItem(title,haveValue,wantValue); 
       inventory.sortedAdd(NewItem); 
       String s= NewItem.toString(); 
       System.out.println(s); 

      } 

      //If the user want to change an item in the inventory 
      else if(command.equalsIgnoreCase("M")) 
      { 
       System.out.println("Please enter the title of the DVD you would like to modify"); 
       title = kb.next(); 

       //Search through the inventory 
       for(int i =1; i<=inventory.size();i++) 
       { 
        StockItem item = (StockItem)inventory.get(i); 

        //If an item is found, print it out and ask the user for a new want value 
        if((item.getTitle()).equals(title)) 
        { 
         System.out.println("Title: " + item.getTitle()); 
         System.out.println("Want value: " + item.getWant()); 
         System.out.println("Have value: " + item.getHave()); 

         System.out.println("Please enter the new want value for " + item.getTitle()); 
         int newWantValue = kb.nextInt(); 

         item.setWant(newWantValue); 

         String s= item.toString(); 
         System.out.println(s); 

        } 
       } 
      } 

      //If the user want to sell an item for the inventory 
      else if(command.equalsIgnoreCase("S")) 
      { 
       System.out.println("Please enter the title of the DVD you would like to sell"); 
       title = kb.next(); 

       //Search through the inventory 
       for(int i =1; i<=inventory.size();i++) 
       { 
        StockItem item = (StockItem)inventory.get(i); 
        if((item.getTitle()).equals(title)) 
        { 
         //if there is non in the inventory, add the user to the waiting list 
         if(item.getHave() == 0) 
         { 
          System.out.println("We are currently out of " + item.getTitle()); 
          System.out.println("Please add customer to waiting list"); 

          System.out.println("Please enter customer's last name"); 
          String lastName = kb.next(); 
          System.out.println("Please enter customer's first name"); 
          String firstName = kb.next(); 
          System.out.println("Please enter customer's phone number"); 
          String phone = kb.next(); 
          item.addToWaitingList(lastName,firstName,phone);    
         } 

         else{ 
          //decrease the have by 1 
           item.setHave(item.getHave()-1);      
          } 

         String s= item.toString(); 
         System.out.println(s); 
        } 
       } 
      } 

      //if the user want to print out a purchase order 
      else if(command.equalsIgnoreCase("O")) 
      { 
       //String s = " Purchase Order: \n"; 
       System.out.println("Purchase Order:"); 
       for(int i =1; i<=inventory.size(); i++) 
       { 
        StockItem item = (StockItem)inventory.get(i); 
        if(item.getHave()<item.getWant()) 
        { 
         int ToBePurchased = item.getWant()+item.getSizeofWaitingList()- item.getHave(); 

         String s = "\n Title: "+ item.getTitle() +"\t"+"Number in stock: "+ item.getHave()+"\t"+"Number of Wants:"+item.getWant()+"\t"+" Number of customers on waiting List : " 
           +item.getSizeofWaitingList()+"\t"+"Number to be Purchased: " +ToBePurchased ; 
         System.out.println(s); 
        } 
       } 
      } 

      //if the user want to a return order 
      else if(command.equalsIgnoreCase("R")) 
      { 
       //Search through the inventory 
       for(int i =1; i<=inventory.size();i++) 
       { 
        StockItem item = (StockItem)inventory.get(i); 

         if(item.getHave()>(item.getWant()+item.getSizeofWaitingList())) 
         { 
          int returnValue = item.getHave() - item.getWant() - item.getSizeofWaitingList(); 
          item.setHave(returnValue); 

          String s= item.toString(); 
          System.out.println(s); 

         } 
         if(item.getHave() == 0 && item.getWant() == 0 && item.getSizeofWaitingList() == 0) 
         { 
          inventory.sortedRemove(item); 

          String s= item.toString(); 
          System.out.println(s); 
         } 
       } 

      } 

     } while (!command.equalsIgnoreCase("Q")); 

    } 
} 
+0

添加一個斷點,看看有什麼title'變量'值當這種情況發生 –

+0

我居然得到它通過創建一個新的輸入掃描儀,需要用戶輸入的每個if語句後工作。 感謝你的想法太 – digitbizzGH

回答

0

我得到它,我增加一個新的輸入掃描器掃描儀輸入=新掃描儀(System.in);後,每使用需要用戶輸入的語句。而不是全球性的。