2016-10-16 13 views
1

私のコードは、すでに宣言されている配列を出力してから、ユーザー入力を求めます。ユーザはxyの形式で番号を入力するか、quitを入力してプログラムの使用を停止することになっています。ユーザからの入力を受けた後、xを行として、yを列番号として使用し、そのインデックスを0に設定して新しい配列を印刷することによって配列の要素を出力します。 私はこれまで、整数だけを受け入れるか、ユーザーから「辞める」ことを別にして、ほとんどを達成しました。ユーザーが「終了」とは別の文字列を入力すると、プログラムがクラッシュします。 これは私のコードです。 import java.util.Scanner;範囲外の文字列インデックス:1 れるjava.langでタイプintまたは文字列のみを受け入れるようにユーザー入力を検証する "quit"

public class Exercise23 { 
    public static void main(String[] args) { 
     Scanner read = new Scanner(System.in); 

     int [][] array = { 
      {0, 1, 4, 5}, 
      {3, 7, 9, 7}, 
      {1, 8, 2, 1} 
     }; 

     for (int i = 0; i < array.length; i++) { 
      for (int j = 0; j < array[i].length; j++) { 
       System.out.print(array[i][j]); 
      } 
      System.out.println(); 
     } 

     boolean exitCon = false; 

     do { 
      System.out.println("Please enter a number in the format 'xy' with no spaces in between or enter 'quit' to stop"); 
      String xy = read.nextLine(); 
      if (!"quit".equals(xy)) { 
       String x = xy.substring(0, 1); 
       String y = xy.substring(1); 
       int row = Integer.parseInt(x); 
       int column = Integer.parseInt(y); 
       if (0 <= row && 0 <= column && row <= 2 && column <=) { 
       System.out.println(); 
        System.out.println(array[row][column]); 
        array[row][column] = 0; 

        System.out.println(); 

        for (int i = 0; i < array.length; i++) { 
         for (int j = 0; j < array[i].length; j++) { 
          System.out.print(array[i][j]); 
         } 
         System.out.println(); 
        } 
        System.out.println(); 

       } else { System.out.println("The number has to be in range 00-23 inclusive considering the format 'xy'."); 

       } 

      } else if (xy.equals("")) { 
       System.out.println("You can only enter integers or 'quit'.");    
      } else { 
       exitCon= true; 
      } 

     } while (!exitCon); 

    } 
} 

問題は、このビット

String xy = read.nextLine(); 
if (!"quit".equals(xy)) { 
    String x = xy.substring(0, 1); 
    String y = xy.substring(1); 
    int row = Integer.parseInt(x); 
    int column = Integer.parseInt(y); 
    if (0 <= row && 0 <= column && row <= 2 && column <= 3) { 
     System.out.println(); 
     System.out.println(array[row][column]); 
     array[row][column] = 0; 

     System.out.println(); 

     for (int i = 0; i < array.length; i++) { 
      for (int j = 0; j < array[i].length; j++) { 
       System.out.print(array[i][j]); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 

     } else { System.out.println("The number has to be in range 00-23 inclusive considering the format 'xy'."); 

     } 

    } else if (xy.equals("")) { 
      System.out.println("You can only enter integers or 'quit'.");    
    } else { 
     exitCon= true; 

私はメインの "java.lang.StringIndexOutOfBoundsExceptionこのエラー「スレッドの例外" を取得しています。 String.substring(String.java:1963) at Exercise23.main(Exercise23.java:26) "

+0

それがクラッシュしたとき、あなたはどのようなエラーが出るのですか? – Steve101

+0

申し訳ありませんエラーメッセージを追加するのを忘れました。今編集されました。 @ Steve101 –

+1

これは、入力文字列( 'next.readLine()')の長さがゼロに等しいことを意味します。それで 'xy.substring(0、1)'が例外をスローする – krzydyn

答えて

0

を作った、これはケースをカバーし、拡張簡単ですし、コードを参照してください。

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in,"UTF-8")); 
     String s; 
     while ((s=br.readLine())!=null) { 
      if (s.equals("quit")) { 
       break; 
      } 
      else if (s.matches("\\d{2}")) { 
       // parse it (exactly 2 digits) 
       System.out.print("parse:"+s); 
      } 
      else { 
       System.out.println("You can only enter 2dig integers or 'quit'."); 
      } 
     } 
0

例外が追加されているため、ここではエラーが発生しているようです。

String x = xy.substring(0, 1); 
String y = xy.substring(1); 

StringIndexOutOfBoundsExceptionを文字列 "XY" はreadlineの上、おそらく空白、短すぎることを意味します。

0

あなたのコードの問題は、 "quit"ではない文字列が最初のif条件に入ることです。これが原因でコードがクラッシュする原因になります。指定された文字列が数値の場合にのみ、最初の条件を入力します。あなたの条件は次のとおりです:

if (StringUtils.isNumeric(xy)) { 
... 
} else if (!xy.equals("quit")) { 
    System.out.println("You can only enter integers or 'quit'."); 
} else { 
    exitCon= true; 
} 
+0

「シンボルが見つかりません」というエラーメッセージが表示されます。 StringUtilsを使用しているとき –

+0

@UysalM 'StringUtils.isNumeric()'はApache Commons Langライブラリのもので、インポートするサードパーティのライブラリです。私はちょうど '.isNumeric(xy)'が非常に自明であるので、論理がどうあるべきかの例としてそれを使用していました。重要な部分は、文字列が数字であるかどうか、サードパーティのライブラリか正規表現のマッチングかその他の方法を使用するかどうかをチェックすることです。 – blitz1616

0

ありがとうございます。私はあなたの答えとコメントから自分の問題を解決することができました。

私は、これらの変更

if (xy.length() == 2) { 
    String x = xy.substring(0, 1); 
    String y = xy.substring(1); 
    int row = Integer.parseInt(x); 
    int column = Integer.parseInt(y); 
    if (0 <= row && 0 <= column && row <= 2 && column <= 3) { 
    System.out.println(); 
     System.out.println(array[row][column]); 
     array[row][column] = 0; 

     System.out.println(); 

     for (int i = 0; i < array.length; i++) { 
       for (int j = 0; j < array[i].length; j++) { 
        System.out.print(array[i][j]); 
       } 
       System.out.println(); 
     } 
     System.out.println(); 

     } else { System.out.println("The number has to be in range 00-23 inclusive considering the format 'xy'."); 

     } 

} else if ("quit".equals(xy)) { 
     exitCon= true;        
} else { 
     System.out.println("You can only enter integers or 'quit'."); 
+0

あなたの解決策はまだ "間違っています"。数字ではない2つの文字を入力することを検討してください。そのため、入力を非常に正確にチェックする必要があります。 – krzydyn

+0

長さを確認した後、数字だけが含まれていることを確認するにはどうすればいいですか?結局のところ、文字列を終了させるために文字を含むことができます。 @krzydyn –

+0

私の答えをご覧ください – krzydyn

関連する問題