2016-11-21 3 views
-1

Das Programm muss ohne Argumente und mit Argumenten ausgeführt werden. Das Programm führt je nach den Eingaben in der Befehlszeile unterschiedliche Aktionen aus. Hier ist der Code, Zeile 94 verursacht das Problem, wo ich Int a geparst habe (int a = Integer.parseInt (args [0]);)Ich bekomme einen ArrayIndexOutOfBoundsException-Fehler wegen der Analyse von int. Aber ich muss das Int analysieren?

Aber ich muss das Int analysieren, um das Programm mit auszuführen fünf oder mehr Befehlszeilenargumente. Hier

ist der Code, es ist lang, aber das Problem ist in Zeile 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); 
     } 
} 
} 

Jede Hilfe wäre sehr dankbar, danke!

+3

'int a = Ganzzahl.parseInt (args [0]);' - Warum parsen Sie args [0], ohne zu prüfen, ob es verfügbar ist? Im Rest Ihres Codes suchen Sie nach 'args.length', bevor Sie auf Elemente dieses Arrays zugreifen. – Eran

+0

Versuchen Sie, den Code zur besseren Lesbarkeit einzurücken. –

+0

Sidenote, die meisten der IDE markieren jetzt diese Art von offensichtlichen Problemen.Ich rate Ihnen, intelliJ oder Eclipse mit ReSharper oder SonarLint zu verwenden, um diese Dinge mit Leichtigkeit zu finden und zu reparieren. (Diese Fehler brauchen nicht einmal ein Codeanalyse-Plugin, sondern es wird direkt von der IDE als Warnung/Fehler markiert.) – Alex

Antwort

0

Sie sollten das Programm, indem Sie return Schlüsselwörter stoppen, wenn Sie keine Parameter zu Ihrem Java Programm bestanden hat.

zB:

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. 

Aber die Hauptidee ist, dass Sie keinen anderen args[..] Wert int analysieren sollen, wenn Ihr agrs[..] Wert gleich 0 ist

0

Ich glaube, dass die Ausgabe is-

Hello 
    World !! 

und danach ist ein Fehler aufgetreten. Dies liegt daran, dass Sie auf das 0th-Argument int a = Integer.parseInt(args[0]) zugegriffen haben, ohne seine Existenz zu überprüfen, und wenn kein Argument dafür angegeben wird, wird der Fehler ausgegeben.

Existenz vor dem Zugriff prüfen.

0

Beim Zugriff auf das Array-Element sicherstellen, dass sein Index Folgendes gewährleistet.

Das Array ist nicht null

Der Index, den Sie zugreifen möchten, werden innerhalb der Array-Größe ist.

In Ihrem Code fügen Sie den Zustand vor dem args Zugriff auf [0] wie unten dargestellt:

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

als versuchen, den int a vor dieser Bedingung zu erklären und hier definieren weitere Fehler zu vermeiden.

Voll Code:

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

Ihre Linie 94 wird ohne Kontrolle der Größe von args genannt. Versuchen Sie zu überprüfen, ob die Größe in Ordnung ist, bevor Sie versuchen, auf die Array-Zelle zuzugreifen.

if(args.length > 1){ 
    int a = Integer.parseInt(args[0]); 
    //What you want to do with you value. 
} 
Verwandte Themen