2016-09-21 9 views
0

私は現在、javaを使用してプログラムに苦労しています。今のところプログラムはすべて良いです。しかし、私は小さな間違いを犯しました。ケース1(アイテムを追加)では、メーカーにスペースを入れて文字列を入力しました。それは私がその問題のために多くの研究を行った。このJavaにスペースを入力してください。 InputMismatchException

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.nextDouble(Unknown Source) 
at ass1.inventory.addItem(inventory.java:44) 
at ass1.inventory.run(inventory.java:28) 
at ass1.inventory.main(inventory.java:11) 

のようにEclipseでエラーが発生し、代わりにin.next()の使用in.nextLine()の追加など、エラーがまだ出てきました。誰かが私のために問題を指摘し、問題を解決することができますか?どうもありがとうございます!!

これは私のコードです:

import java.util.Scanner; 

public class inventory { 

private static item[] inventory; 
static Scanner scanner = new Scanner(System.in); 
private static int noOfItems; 

public static void main(String[] args) { 
    noOfItems=0;   
    // TODO Auto-generated method stub 
    inventory=new item[5]; 
    run();  
} 
public static int displayMenu() 
{ 
    System.out.println("1.Add a product"); 
    System.out.println("2.Display a product"); 
    System.out.println("3.Quit"); 
    int i=scanner.nextInt(); 
    return i; 
} 
public static void run() 
{ 
    while(true) 
    { 
     int i=displayMenu(); 
     switch(i) 
     { 
     case 1:addItem(); 
       break; 
     case 2:findItem(); 
       break; 
     case 3:return; 
     default:System.out.println("Invalid choice"); 
     } 
    } 
} 
public static void addItem() 
{ 
    System.out.print("Enter Item name:"); 
    String item_name=scanner.next(); 
    System.out.print("Enter the manufacturer:"); 
    String manufacturer=scanner.next(); 
    System.out.print("Enter price:"); 
    double price=scanner.nextDouble(); 

    item b=new item(item_name,manufacturer,price); 
    if(noOfItems==inventory.length) 
     System.out.println("Array is full"); 
    else 
    { 
     inventory[noOfItems++]=b; 
     System.out.println("Item added successfully"); 
    } 
} 
public static void findItem() 
{ 
    System.out.print("Enter item name:"); 
    String item_name=scanner.next(); 
    for(int i=0; i<noOfItems; i++) 
    { 
     if(item_name(inventory[i].getItem_name())) 
     { 
      System.out.println("Item found:"); 
      System.out.print(inventory[i]+"\n"); 
      return; 
     }       
    }  
} 
} 

これは別のファイルには、最初の1に接続している:

public class item { 

    private String item_name; 
    private String manufacturer; 
    private double price; 

    //To initialise the state of the object 
    public item(String item_name,String manufacturer,double price) 
    { 
    this.item_name=item_name; 
    this.manufacturer=manufacturer; 
    this.price=price; 
    } 
    //Reader methods i.e behavior methods 
    public String getItem_name() 
    { 
    return item_name; 
    } 
    public String getManufacturer() 
    { 
    return manufacturer; 
    } 
    public double getPrice() 
    { 
    return price; 
    } 
    //Writer methods or setter methods public void setTitle(String item_name) 
    { 
    this.item_name=item_name; 
    } 
    public void setManufact(String manufacturer) 
    { 
    this.manufacturer=manufacturer; 
    } 
    public void setPrice(double price) 
    { 
    if(price < 0) 
     System.out.println("Price cannot be negative"); 
    else 
     this.price=price; 
    } 
    public String toString() 
    { 
    return "Item name:"+item_name+"\nManufacturer:"+manufacturer+"\nPrice:"+price; 
    } 
} 

答えて

0

あなたは多くの点で

を問題を解決することができ先頭にscanner.useDelimiter(System.getProperty("line.separator"));を追加してみてくださいadditemの方法。

0

scanner.next()はスペースの前に次のトークンのみを読み込むため、エラーが発生しています。

コードを実行していて、値を表示するための少しの変更を加えました。以下のように。

System.out.print("Enter Item name:"); 
String item_name = scanner.next(); 
System.out.print("Enter the manufacturer:"); 
String manufacturer = scanner.next(); 
System.out.println("Manufacturer:" + manufacturer); //Print Manufacturer 
System.out.print("Enter price:"); 
double price = scanner.nextDouble(); 

そして私はメーカーの入力としてtest testを書くとしたら、唯一testは、変数のメーカーが割り当てられます。残りの文字列をバッファに残します。以下の結果と同様です。

Enter Item name:test 
Enter the manufacturer:test test 
Manufacturer:test 
Enter price: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.nextDouble(Unknown Source) 
    at stackOverflowQns.inventory.addItem(inventory.java:50) 
    at stackOverflowQns.inventory.run(inventory.java:32) 
    at stackOverflowQns.inventory.main(inventory.java:15) 

あなたはscanner.nextLine()を使用していると言いましたが動作しません。これが理由です。

scanner.next(),scanner.nextInt()などのメソッドを使用しているので、これらのメソッドはすべてバッファにラインセパレータ(\n)を残します。したがって、後でscanner.nextLine()を呼び出すと、行区切り記号の前にあるものがすべて取り出され、空の文字列が返されます。そして、それはあなたがこのようなことを見る場所です。したがって、あなたの問題を解決する

Enter Item name:test 
Enter the manufacturer:Enter price: 

、あなただけの行区切りをクリアするには、ユーザから値を読む前scanner.nextLine()を行うにはどちらか持って 。以下のように。しかし、これはちょうど醜いです、私はお勧めしません。

System.out.print("Enter the manufacturer:"); 
scanner.nextLine(); 
String manufacturer = scanner.nextLine(); 

またはすべての言及の問題を解決し、それゆえ、唯一の区切り文字として行区切りを使用するように設定することである、他の答えは、提案するもののようにしてください。

関連する問題