2016-03-21 2 views
0

JavaのListに対してIndexOutOfBoundsExceptionをキャッチしようとすると、問題が発生していました。その後、私はトライキャッチを実行しようとしましたJava try catchが処理されないIndexOutOfBoundsException

List<String> list = new ArrayList<>(Arrays.asList("item1", "item2")); 

::私はオプションのために2よりも大きな何かを入力したときに

do { 
    for (int i = 0; i < list.size(); i++) { 
     System.out.print("(" + (i + 1) + ")" + list.get(i)); 
    } 
    System.out.println(" "); 

    try{ 
     option = sc.nextInt(); 
    } catch (IndexOutOfBoundsException e){ 
     System.out.println("Invalid option"); 
     sc.next(); 
     continue; 
    } catch (InputMismatchException e) { 
     System.out.println("Option input mismatch."); 
     sc.next(); 
     continue; 
    } 
    sc.nextLine(); 
    if (option == 1) { 
     System.out.print("Enter name: "); 
     // scanner takes in input 
    } else if (option == 2) { 
     System.out.print("Enter desc: "); 
     // scanner takes in input 
    } 
type = list.get((option - 1)); 
} while (option <= 0 || option >= 3); 

しかし、それは例外を私にIndexOutOfBounds投げたので、私は2つの要素との私のリストを宣言しました私はすでにそれを試してみたと思った?

ありがとうございます。

+2

投稿したコードの中で 'list'は何もしていませんか? – brso05

+0

'nextInt'が' IndexOutOfBoundsException'を投げるのはなぜですか?実際にそれを投げたコードを含めてください。 – rgettman

+0

例外があなたの 'list'から来ている場合、' try'ブロックは 'list'を使って例外を捕まえることができるコードを含んでいなければなりません。 – khelwood

答えて

0
do { 
     for (int i = 0; i < list.size(); i++) { 
      System.out.print("(" + (i + 1) + ")" + list.get(i)); 
     } 
     System.out.println(" "); 

     try { 
      option = sc.nextInt(); 
     } catch (IndexOutOfBoundsException e) { 
      System.out.println("Invalid option"); 
      sc.next(); 
      continue; 
     } catch (InputMismatchException e) { 
      System.out.println("Option input mismatch."); 
      sc.next(); 
      continue; 
     } 
     sc.nextLine(); 
     if (option == 1) { 
      System.out.print("Enter name: "); 
      // scanner takes in input 
     } else if (option == 2) { 
      System.out.print("Enter desc: "); 
      // scanner takes in input 
     } 
     try { 
      type = list.get((option - 1)); 
     } catch (IndexOutOfBoundsException e) { 
      System.out.println("Invalid option"); 
      option=3; 
     } 
    } while (option <= 0 || option >= 3); 

私はタイプ= list.get((オプション - 1))で新しいのtry-catchを追加しました。 ユーザーの再入力オプションを強制するために、私はキャッチ原因で3にオプションを設定します

+0

こんにちは、それは私が思ったものですが、ユーザーに再度入力を促すものではありません。私が2より大きい値を入力したときと同じように、最初のforループの部分はもう一度表示されずに入力を要求します –

+0

コンソールには何が入っていますか? – VinhNT

+0

ちょうど空のスペースと私は任意の値を入力する必要がありますし、その部分は後に出てくるループです。ああさて、私はすでにsc.next()が最後に試してキャッチしているので、それは知っていた –

1

無効な値を使用してリストを呼び出すことはできません。

ArrayList<String> list = new ArrayList<>(Arrays.asList("item1", "item2")); 
    Scanner sc = new Scanner(System.in); 
    int option; 
    try { 
     option = sc.nextInt(); 
     System.out.println(list.get(option)); 
    } catch (IndexOutOfBoundsException e) { 
     System.out.println("Invalid option"); 
    } catch (InputMismatchException e) { 
     System.out.println("Option input mismatch."); 
    } 

    sc.close(); 
+0

私はこの問題がこの行にあることを知りました:type = list.get((option-1));.私はそれをtry catchで囲んだ後、エラーはなくなりましたが、ユーザーが2より大きい何かを入力したときに再び入力を求めるメッセージが表示されませんでした –

+0

こんにちは。 –

0

ます。また、このようにそれを行うことができますが、有効な値を入力するまで、有効な値が名前を尋ねる入力された後、それがループます(実装されていない)

ArrayList<String> list = new ArrayList<>(Arrays.asList("item1", "item2")); 
    Scanner sc = new Scanner(System.in); 
    int option = 0; 
    while(!(option == 1 || option==2)) { 

     try { 
      option = sc.nextInt();    
     } catch (InputMismatchException e) { 
      System.out.println("Option input mismatch."); 
     } 
    } 
    System.out.println(list.get(option-1)); 
    sc.close(); 
関連する問題