2016-03-02 54 views
0

需要帮助一些有这个错误,我不能把它搞清楚。该程序假设读取输入并保存。异常在线程“main”中java.util.InputMismatchException 在java.util.Scanner.throwFor处扫描(Scanner.java:864) 在java.util.Scanner.next处扫描(Scanner.java:1485) ÏϧÏ在java.util.Scanner.nextDouble(Scanner.java:2413)在Driver.main(Driver.java:112) ÏÏ§Ï ÏϧÏ帮助修复错误。线程“主”异常java.util.InputMismatchException

=================================== 

    import java.io.*; 
    import java.util.Scanner; 
    public class Driver 

    { 
     public static void main(String[] args) 

    { 
     //local constants 

     //local variables 
     String fileName = "items.txt"; 
     Scanner scanner = null; 
     ItemsList itemsList = new ItemsList(5); 
     int i = 0; 
     int choice; 
     boolean repeat = true;  
     String itemName; 
     double price; 
     int qty; 

     scanner=new Scanner(System.in); 

//****************************************************************************  
     //open the file and catch the exception if file not found 
     try 
     {   
      //create an instance of scanner 
      scanner = new Scanner(new File(fileName)); 

      //read file items until end of file 
      while(scanner.hasNext()) 
      { 
       itemName = scanner.next(); 
       price = scanner.nextDouble(); 
       qty=scanner.nextInt(); 

       //Add the OneItem object to the itemList 
       itemsList.addItem(new OneItem(itemName, price, qty)); 
       i++; 
      } 

      //close the file object 
      scanner.close();   
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println(e.getMessage()); 
     } 
     //Create an instance of Scanner class 
     scanner=new Scanner(System.in); 

     while(repeat) 
     { 
      //call menu 
      choice = menu(); 
      switch(choice) 
      { 
      //Add an item to the itemsList 
      case 1: 
       System.out.println("Enter item name : "); 
       //read name 
       String name=scanner.nextLine(); 

       System.out.println("Enter price : "); 
       //read string value and parse to double value 
       price = Double.parseDouble(scanner.nextLine()); 

       System.out.println("Enter quantity : "); 
       qty=Integer.parseInt(scanner.nextLine()); 

       //Add the OneItem to the itemsList 
       itemsList.addItem(new OneItem(name, price, qty)); 
       break; 

      case 2: 
       //print the list 
       //print heading with specific formatter 
       System.out.printf("%-10s%-10s%-10s\n\n", "Item","Price","Quantity"); 
       System.out.println(itemsList.toString()); 
       break; 

      case 3: 
       //Terminate the program 
       System.out.println("Terminate the program."); 

       //set repeat to false 
       repeat=false; 
       break; 

      default: 
       System.out.println("Incorrect option is selected."); 
       break; 
      }  
     } 
     writeToFile(itemsList); 
    } 




    private static void writeToFile(ItemsList itemsList) 

    { 

     //Create a file name called items.txt 
     String filename="items.txt"; 
     //Create a variable of Class PrintWriter 
     PrintWriter filewriter=null; 

     try 
     { 
      //create an instance of PrintWriter 
      filewriter=new PrintWriter(new File(filename)); 

      //close the file writer 
      filewriter.close(); 
     } 
     catch (IOException e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 

    private static int menu() 


    { 
     //Create an instance of Scanner class 
     Scanner keyboard=new Scanner(System.in); 
     System.out.println("Menu"); 
     System.out.println("1. Add item"); 
     System.out.println("2. Display items"); 
     System.out.println("3. Exit"); 
     System.out.println("Enter your choice"); 
     int choice=Integer.parseInt(keyboard.nextLine()); 
     return choice; 



    } 
    }//end Driver class 

========================================== 

public class OneItem 
{ 
    //declare a variables 
    private String name; 
    private double price; 
    private int quantity; 

    //default constructor 
    public OneItem() 
    { 
     name = ""; 
     price = 0; 
     quantity = 0; 
    } 
    //parameter constructor 
    public OneItem(String name, double price, int quantity) 
    { 
     this.name = name; 
     this.price = price; 
     this.quantity = quantity; 
    } 

    //toString 
    public String toString() 
    {  
     return String.format("%-10s%-10.2f%-10d\n", name,price,quantity); 
    } 
}//end of the OneItem class 

====== =====================

public class ItemsList 
{ 
    //declare variables 
    private OneItem items[]; 
    private int size; 
    private int count; 

    //constructor to set items, size and count to zero 
    public ItemsList() 
    { 
     items = null; 
     size = 0; 
     count = 0; 
    } 
    //Parameter constructor 
    public ItemsList(int size) 
    { 
     items = new OneItem[size]; 

     for (int i = 0; i < items.length; i++) 
     { 
      items[i] = new OneItem(); 
     } 

     this.size = size; 
     count = 0; 
    } 
    //Add OneItem to the itemlist 
    public void addItem(OneItem item) 
    { 
     if(items.length == count) 
     { 
      resize(); 
      items[count] = item; 
      count++; 
     } 
     else 
     { 
      items[count] = item; 
      count++; 
     }  
    } 

    //Resize 
    private void resize() 
    { 
     int oldsize = size; 
     count = oldsize; 
     int newsize = 2 * this.size; 
     size = newsize; 

     OneItem[] tempList = new OneItem[size]; 

     for (int i = 0; i < oldsize; i++)  
      tempList[i] = items[i]; 


     items = new OneItem[size]; 
     items = tempList;  
    } 

    //getSize 
    public int getSize() 
    { 
     return count; 
    } 

    //toString 
    public String toString() 
    { 
     String description = ""; 
     for (int i = 0; i <count; i++) 
     { 
      description += items[i].toString(); 
     } 

     return description; 
    } 
} 
+0

1)不是一个调试服务,2)没有足够的详细信息发生错误,3)我试过了,当你输入有效的输入时,它对我很好。在某个地方的意思是,你输入不好的字符,它会中断,因此验证你的输入。 – codeCompiler77

回答

0

未来,您需要更清楚问题是什么。它是如何发生的。错误输入字符串转换成购买价格时,我得到的是:

Exception in thread "main" java.lang.NumberFormatException: For input string: "sdcf" 
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) 
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) 
    at java.lang.Double.parseDouble(Double.java:538) 
    at stacktests.Driver.main(Driver.java:70) 

含义你验证您输入的。例如:

Scanner sc = new Scanner(System.in); 
while (!sc.hasNextInt()) 
{ 
    System.out.println("Enter an integer!"); 
    sc.nextLine(); 
} 
int num = sc.nextInt(); 
System.out.println("Thank you! (" + num + ")"); 

您需要禁止将字符串输入到数字变量中!只有输入一经验证,您是否将其分配给一个变量。

干杯队友。

相关问题