2017-02-17 4 views
0

Scannerに問題があります。入力値の型を取っていると思われ、次回ユーザーが同じ型の値を入力したように見えます。私はこのコードが動作していない理由を見つけることができず、私はこの100万回のようなコードを書いて問題がなかったので、私にInputMismatchExceptionを与えます。この問題は、ちょうどこのコードでレジスタ()メソッドが、プログラム全体、例えばのためではありませんInputMismatchExceptionの理由が見つかりません

public void register(){ 
    Scanner input=new Scanner(System.in); 
     System.out.println("What course would you like to register for?"); 
     String course_name = input.next(); 
     System.out.println("What section?"); 
     int section = input.nextInt(); 

     for (int i = 0; i < courses.size(); i++) { 
      if (courses.get(i).getCourse_name().equals(course_name)) { 
       if (courses.get(i).getCourse_section() == section) { 
        courses.get(i).AddStudent(this.first_name+" "+this.last_name); 
       } 
      } 
     } 
     input.close(); 
    } 

は:

public void Options() { 
    Scanner input=new Scanner(System.in); 
    while (true) { 
     System.out.println("What would you like to do (Enter corresponding number):" + "\n" + "1) View all courses" + "\n" + "2) View all courses that are not full" + "\n" + "3) Register on a course" + "\n" + "4) Withdraw from a course" + "\n" + "5) View all courses that the current student is being registered in" + "\n" + "6) Exit"); 
     int user = input.nextInt(); 
     if (user == 1) 
      viewAll(); 
     if (user == 2) 
      viewAllOpen(); 
     if (user == 3) 
      register(); 
     if (user == 4) 
      withdraw(); 
     if (user == 5) 
      viewRegistered(); 
     if (user == 6) { 
      Serialize(); 
      break; 
     } 
    } 

レジスタ等の方法の一つは、入力するように、ユーザが必要な場合文字列、intユーザー= input.nextInt(); InputMismatchExceptionが発生します。

+1

チェックhttp://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – TheLostMind

答えて

0

このコードを再現しましたが、同じ問題は発生していません。コース番号の入力を求めるプロンプトが表示されたときに11などの整数を入力すると、コードはうまく動作します。もちろん、整数でないものを入力すると、InputMismatchExceptionがスローされます。 Scanner#nextInt()のJavaドキュメントの説明、特にこれを参照してください。

入力の次のトークンをintとしてスキャンします。

nextInt()という形式のこのメソッドの呼び出しは、nextInt(radix)の呼び出しとまったく同じように動作します。radixは、このスキャナのデフォルトの基数です。

例外:

InputMismatchExceptionを - 次のトークンがInteger正規表現に一致しない場合、またはあなたがこれを防ぎたいとしたくない場合は範囲​​

Read More

の外にありますtry-catchを処理するには、有効な整数が与えられるまで実行を一時停止することができます。

public static void register(){ 
    Scanner input=new Scanner(System.in); 
    System.out.println("What course would you like to register for?"); 
    String course_name = input.next(); 
    System.out.println("What section?"); 
    //Loop until the next value is a valid integer. 
    while(!input.hasNextInt()){ 
     input.next(); 
     System.out.println("Invalid class number! Please enter an Integer."); 
    } 
    int section = input.nextInt(); 
    input.close(); 

    System.out.println(course_name + " " + section); 
} 
+0

それは整数を入力しない問題ではありません。私がそれを実行すると、登録したいコースの名前を入力してプログラムが停止した直後にInputMismatchExceptionが発生するため、整数を入力する機会さえも得られません。 – UnionSquareBanter

+0

要求された文字列をスキャンするとすぐに、オプションの方法でスキャナを閉じてみてください。リソースがまだオープンしているという事実は、登録メソッドで開かれているスキャナを妨害しています。その場合、スタックトレースを貼り付けてください。 'スキャナ入力=新しいスキャナ(System.in); while(true){ System.out.println( "blah"); int user = input.nextInt(); input.close() //その他のコードはここにあります。 –

関連する問題