2012-03-24 3 views
0

私は大きな数のプログラムを追加しています(bigintegerクラスを使用しないで)。メインArrayIndexOutOfBoundsException大きな番号のプログラムを追加する

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 
    at LargeInteger.add(LargeInteger.java:50) 
    at testLargeInteger.main(testLargeInteger.java:32) 

import java.util.Scanner; 

public class testLargeInteger 
{ 



public static void main(String[] args) 
    { 
    Scanner input = new Scanner(System.in); 
     String string1; 
     String string2; 
     int exp =0; 


     System.out.print("Enter the first integer: "); 
     //Store up the input string “string1” entered by the user from the keyboard. 
     string1 = input.next(); 

     LargeInteger firstInt = new LargeInteger(string1); 

     System.out.print("Enter the second integer: "); 
     string2 = input.next(); 
     //Store up the input string “string2” entered by the user from the keyboard. 
     LargeInteger secondInt = new LargeInteger(string2); 

     System.out.print("Enter the exponential integer: "); 
     //Store up the input integer “exp” entered by the user from the keyboard. 
     exp = input.nextInt(); 


     LargeInteger sum = firstInt.add(secondInt); 

     System.out.printf ("First integer: %s \n", firstInt.display()); 
     System.out.println("Second integer: " + secondInt.display()); 
     System.out.println(" Exponent: " + exp); 

     System.out.printf (" Sum = %s \n", sum.display()); 

    } 
} 

LargeIntegerクラス:

public class LargeInteger { 


    private int[] intArray; 


    //convert the strings to array 
    public LargeInteger(String s) { 
     intArray = new int[s.length()]; 
     for (int i = 0; i < s.length(); i++) { 
      intArray[i] = Character.digit(s.charAt(i), 10); // in base 10 
     } 
    } 

    public LargeInteger(int[] array) { 
     intArray = array; 
    } 

    //display the strings 
    public String display() {   
      String result=""; 

      for (int i = 0; i < intArray.length; i++) {  
      result += intArray[i]; 
      } 
      return result.toString(); 
     } 

    //get first array 
    public int[] getIntArray() { 
      return intArray; 
     } 

    public LargeInteger add(LargeInteger secondInt){ 

     int[] otherValues = secondInt.getIntArray(); 

     int maxIterations = Math.min(intArray.length, otherValues.length); 
     int currentResult; //to store result 
     int[] resultArray = new int[Math.max(intArray.length, otherValues.length) + 1]; 

     int needToAdd = 0; //to store result should be added next step 

     for(int i = 0; i < maxIterations; i++) { 
      currentResult = intArray[i] + otherValues[i]; 
      resultArray[i] = currentResult % 10 + needToAdd; //if more than 9 its correct answer 
      needToAdd = currentResult/10; //this is what you need to add on next step 
     } 

     resultArray[Math.max(intArray.length, otherValues.length) + 1] = needToAdd; 

     return new LargeInteger(resultArray); 

    } 

} 
+0

LargeIntegerクラスで発生しているエラーは何行ですか? –

+0

downvoter:質問は明確ではありませんか?研究努力はしていませんか?役に立たない?はいの場合 - コメントしてください - そうでない場合:あなたはダウンボートしないでください。あなたに質問が "ばかげている"ように見える場合 - ダウンボートする理由ではありません。 – amit

答えて

3

ここでは、長さの配列を宣言:

int[] resultArray = new int[Math.max(intArray.length, otherValues.length) + 1]; 

、ここであなたが同じ長さ使用してアクセス:(一般およびコンピュータ言語)Javaでは

resultArray[Math.max(intArray.length, otherValues.length) + 1] = needToAdd; 

を、配列のインデックスはで始まります0になり、長さは1に終わります。したがって、配列を10要素長く宣言すると、インデックスは0-9になります。したがって、1つを引く必要があります。

resultArray[Math.max(intArray.length, otherValues.length)] = needToAdd; 
+0

"一般的にコンピュータ言語"は真実ではありません。私が正しく覚えていれば、パスカル配列は0ではなく1から始まります。このステートメントを削除してください。 – amit

+0

「いつも」ではなく「一般的に」と言いました。私は、このようなことが最も一般的ですが、ルールではありません。 –

+0

ありがとう!私はちょうど今余分な0を得ているので40 + 40 = 800しかし、私はこれを把握することができると思う。 – Sackling

2
resultArray[Math.max(intArray.length, otherValues.length) + 1] = needToAdd; 

配列私は、これは理解していたが、何らかの理由で、私は私のaddメソッドのためArrayIndexOutOfBoundsExceptionを取得していますと思いましたjavaの場合は0から始まり、割り振られたスペースは要素の数と同じです:

int[] resultArray = new int[Math.max(intArray.length, otherValues.length) + 1]; 

したがって、配列から1つの要素にアクセスするため、インデックスが不足します。

+0

もう一度お返事ありがとうございます!あなたは私が言わなければならない大きな助けをしてくれました。 – Sackling

関連する問題