2016-11-21 6 views
-1

プログラムは引数と引数で実行する必要があります。プログラムは、コマンドラインの入力に応じて異なる処理を行います。ここではコードがありますが、94行目でintを解析した問題が発生しています(int a = Integer.parseInt(args [0]);)intを解析するためにArrayIndexOutOfBoundsExceptionエラーが発生します。しかし、私はそのintを解析する必要がありますか?

しかし、私はそのintを解析してプログラムを実行する必要があります5つ以上のコマンドライン引数ここで

はそれは長いですが、コードですが、問題はライン94である:

class CommandArgsOrNot{ 
public static void main(String[] args) { 
    final int clargs = args.length; 
     if (clargs==0) { 
      System.out.print(" "); 
      System.out.println("Hello"); 
      for (int i = 0; i < 3; i++) { 
       System.out.print(" "); 
       } 
      System.out.println("World !!"); 
     } 

     if (clargs==1) { 
      String s = args[0]; 
      for (int i = 0; i < s.length(); i++) { 
       for (int j = 0; j < i; j++) { 
        System.out.print(" "); 
       } 
      System.out.println(s); 
      } 
     } 

     if (clargs==2) { 
      System.out.println("ARGUMENT 1:" + " " + (args[0])); 
      System.out.println("ARGUMENT 2:" + " " + (args[1])); 
     } 

     if (clargs==3) { 
     final int a = Integer.parseInt(args[0]); 
     final int b = Integer.parseInt(args[1]); 
     final int c = Integer.parseInt(args[2]); 

     if (a * b == c) { 
      System.out.println("1*2=3"); 
     } 
     else if (a * c == b) { 
     System.out.println("1*3=2"); 
     } 
     else if (b * a == c) { 
     System.out.println("2*1=3"); 
     } 
     else if (b * c == a) { 
     System.out.println("2*3=1"); 
     } 
     else if (c * a == b) { 
     System.out.println("3*1=2"); 
     } 
     else if (c * b == a) { 
     System.out.println("3*2=1"); 
     } 
     else { 
     System.out.println("None"); 
     } 
     System.out.println(); 
     } 

     if (args.length == 4) { 
     int a = Integer.parseInt(args[0]); 
     int b = Integer.parseInt(args[1]); 
     int c = Integer.parseInt(args[2]); 
     int d = Integer.parseInt(args[3]); 
     if (a == b && a == c && a == d) { 
      System.out.println("1"); 
     } 
     else if (a != b && b != c && c != d) { 
      System.out.println("4"); 
     } 
     else if (a == b) { 
      System.out.println("2"); 
     } 
     else if (c == d && a != b) { 
      System.out.println("3"); 
     } 
     else if (b == c) { 
      System.out.println("2"); 
     } 
     else if (a == b && a == c) { 
      System.out.println("3"); 
     } 
     else if (d == c && d == b) { 
      System.out.println("3"); 
     } 
     else if (a == d) { 
      System.out.println("2"); 
     } 
     else if (a == c && b == d) { 
      System.out.println("2"); 
     } 
     else if (a == d && b != c) { 
     System.out.println("3"); 
     } 
     } 

     int a = Integer.parseInt(args[0]); 
     long answer = 1; 
     long result = 0; 
     if (a < 0) { 
      for(int i = 1; i < args.length; i++) { 
       answer *= Integer.parseInt(args[i]); 
      } 
      System.out.println(answer); 
     } 
     else if (a == 0) { 
      for(int i = 0; i < args.length; i++) { 
       result += Integer.parseInt(args[i]); 
      } 
      System.out.println(result); 
     } 
     else if (a == 1) { 
      for(int i = 0; i < args.length; i++) { 
       result += Integer.parseInt(args[i]); 
      } 
      System.out.println(result); 
     } 
     else if (a == 2) { 
      for(int i = 2; i < args.length; i++) { 
       if (i % 2 != 0){ 
       answer += Integer.parseInt(args[i]); 
       } 

      } 
      System.out.println(answer); 
     } 
     else if (a == 3) { 
      for (int i = 2; i < args.length; i++) { 
       if (i % 3 != 0) { 
       answer += Integer.parseInt(args[i]); 
       } 
      } 
      System.out.println(answer); 
     } 
} 
} 

任意の助けをいただければ幸いです、ありがとう!

+3

'int a = Integer.parseInt(args [0]);' - なぜargs [0]が利用可能かどうかをチェックせずに解析しますか?残りのコードでは、その配列の要素にアクセスする前に 'args.length'をチェックします。 – Eran

+0

可読性のためにコードをインデントしてみてください。 –

+0

Sidenote、IDEのほとんどは、この種の明白な問題を強調しています。ReSharperやSonarLintを使ってIntelliJやEclipseを使ってこれらのことを簡単に見つけて修正することをお勧めします。 (これらのエラーには、コード解析プラグインは必要ありません。IDEで直接警告/エラーとしてマークされています) – Alex

答えて

0

プログラムをJavaプログラムに渡さなかった場合は、returnキーワードを使用してプログラムを停止する必要があります。

例:

if (clargs==0) { 
      System.out.print(" "); 
      System.out.println("Hello"); 
      for (int i = 0; i < 3; i++) { 
       System.out.print(" "); 
       } 
      System.out.println("World !!"); 

      return; 
     } //please note that there is also many alternative ways to control your program. 

しかし、主なアイデアは、あなたのagrs[..]値が等しい場合は0

0

は、私は信じていintに他のargs[..]値を解析べきではないということである

は - あなたの出力

この後、エラーが発生します。 これは、int a = Integer.parseInt(args[0])の0番目の引数にその存在をチェックせずにアクセスし、引数が指定されていない場合はエラーをスローするためです。

アクセスする前に存在を確認してください。

0

配列要素のインデックスにアクセスしている間は、次のことを確認してください。

配列がアクセスしようとしているインデックスが配列サイズの範囲内である

nullではありません。あなたのコードで

[0]は、以下に示すように引数にアクセスする前に条件を追加します。

if(clargs!=0) int a = Integer.parseInt(args[0]); 

ALSは、この条件の前のint aを宣言し、さらにエラーを回避するために、ここで定義してみてください。

全コード:

class VariousCases 
    { 
     public static void main(String[] args) 
     { 
      final int clargs = args.length; 
      if (clargs==0) 
      { 
       System.out.print(" "); 
       System.out.println("Hello"); 
       for (int i = 0; i < 3; i++) 
       { 
        System.out.print(" "); 
       } 
       System.out.println("World !!"); 
      } 

      if (clargs==1) 
      { 
       String s = args[0]; 
       for (int i = 0; i < s.length(); i++) 
       { 
        for (int j = 0; j < i; j++) 
        { 
         System.out.print(" "); 
        } 
       System.out.println(s); 
       } 
      } 

      if (clargs==2) 
      { 
       System.out.println("ARGUMENT 1:" + " " + (args[0])); 
       System.out.println("ARGUMENT 2:" + " " + (args[1])); 
      } 

      if (clargs==3) 
      { 
       final int a = Integer.parseInt(args[0]); 
       final int b = Integer.parseInt(args[1]); 
       final int c = Integer.parseInt(args[2]); 

       if (a * b == c) { 
        System.out.println("1*2=3"); 
       } 
       else if (a * c == b) { 
        System.out.println("1*3=2"); 
       } 
       else if (b * a == c) { 
        System.out.println("2*1=3"); 
       } 
       else if (b * c == a) { 
        System.out.println("2*3=1"); 
       } 
       else if (c * a == b) { 
        System.out.println("3*1=2"); 
       } 
       else if (c * b == a) { 
        System.out.println("3*2=1"); 
       } 
       else 
       { 
        System.out.println("None"); 
       } 
       System.out.println(); 
      } 

      if (args.length == 4) { 
       int a = Integer.parseInt(args[0]); 
       int b = Integer.parseInt(args[1]); 
       int c = Integer.parseInt(args[2]); 
       int d = Integer.parseInt(args[3]); 
       if (a == b && a == c && a == d) { 
        System.out.println("1"); 
       } 
       else if (a != b && b != c && c != d) { 
        System.out.println("4"); 
       } 
       else if (a == b) { 
        System.out.println("2"); 
       } 
       else if (c == d && a != b) { 
        System.out.println("3"); 
       } 
       else if (b == c) { 
        System.out.println("2"); 
       } 
       else if (a == b && a == c) { 
        System.out.println("3"); 
       } 
       else if (d == c && d == b) { 
        System.out.println("3"); 
       } 
       else if (a == d) { 
        System.out.println("2"); 
       } 
       else if (a == c && b == d) { 
        System.out.println("2"); 
       } 
       else if (a == d && b != c) { 
        System.out.println("3"); 
       } 
      } 

      // Add the condition to check the arguments length is greater and the index you are trying to access is within the range of the array length 
      if(clargs!=0) 
       int a = Integer.parseInt(args[0]); 
      long answer = 1; 
      long result = 0; 
      if (a < 0) { 
       for(int i = 1; i < args.length; i++) { 
        answer *= Integer.parseInt(args[i]); 
       } 
       System.out.println(answer); 
      } 
      else if (a == 0) { 
       for(int i = 0; i < args.length; i++) { 
        result += Integer.parseInt(args[i]); 
       } 
       System.out.println(result); 
      } 
      else if (a == 1) { 
       for(int i = 0; i < args.length; i++) { 
        result += Integer.parseInt(args[i]); 
       } 
       System.out.println(result); 
      } 
      else if (a == 2) { 
       for(int i = 2; i < args.length; i++) { 
        if (i % 2 != 0){ 
        answer += Integer.parseInt(args[i]); 
        } 

       } 
       System.out.println(answer); 
      } 
      else if (a == 3) { 
       for (int i = 2; i < args.length; i++) { 
        if (i % 3 != 0) { 
        answer += Integer.parseInt(args[i]); 
        } 
       } 
       System.out.println(answer); 
      } 
    } 
    } 
0

あなたのライン94は、引数のサイズ上の任意のチェックなしで呼ばれています。アレイセルにアクセスしようとする前にサイズが問題ないかどうかを確認してください。

if(args.length > 1){ 
    int a = Integer.parseInt(args[0]); 
    //What you want to do with you value. 
} 
関連する問題