2014-11-22 72 views
0

我正在做一個任務,其中的目標是,除其他外,添加兩個大整數。這是我的代碼,分成四個文件。NumberFormatException:對於輸入字符串:「[memorylocation」java

主要是我們無法改變的:

import java.util.*; 
import MyUtils.MyUtil; 

public class CSCD210HW7 
{ 
    public static void main(String [] args)throws Exception 
    { 
     int choice; 
     String num; 
     LargeInt one, two, three = null; 
     Scanner kb = new Scanner(System.in); 

     num = HW7Methods.readNum(kb); 
     one = new LargeInt(num); 

     num = HW7Methods.readNum(kb); 
     two = new LargeInt(num); 

     do 
     { 
     choice = MyUtil.menu(kb); 

     switch(choice) 
     { 
      case 1: System.out.println(one + "\n"); 
        break; 

      case 2: System.out.println("The value of the LargeInt is: " + two.getValue() + "\n"); 
        break; 

      case 3: num = HW7Methods.readNum(kb); 
        one.setValue(num); 
        break; 

      case 4: if(one.equals(two)) 
         System.out.println("The LargeInts are equal"); 
        else 
         System.out.println("The LargeInts are NOT equal");   
        break; 

      case 5: three = two.add(one); 
        System.out.printf("The results of %s added to %s is %s\n", one.getValue(), two.getValue(), three.getValue()); 
        break; 

      case 6: HW7Methods.displayAscendingOrder(one, two, three); 
        break; 

      default: if(two.compareTo(one) < 0) 
         System.out.printf("LargeInt %s is less than LargeInt %s\n", two.getValue(), one.getValue()); 
        else if(two.compareTo(one) > 0) 
         System.out.printf("LargeInt %s is greater than LargeInt %s\n", two.getValue(), one.getValue()); 
        else 
         System.out.printf("LargeInt %s is equal to LargeInt %s\n", two.getValue(), one.getValue());  
        break; 
     }// end switch 

     }while(choice != 8); 

    }// end main 

}// end class 

LargeInt類(自定義類,我們創建)

public class LargeInt implements Comparable<LargeInt> 
{ 
    private int[]myArray; 

    private LargeInt() 
    { 
     this("0"); 
    } 

    public LargeInt(final String str) 
    { 
     this.myArray = new int[str.length()]; 
     for(int x = 0; x < this.myArray.length; x++) 
     { 
     this.myArray[x] = Integer.parseInt(str.charAt(x)+ ""); 
     } 
    } 

    public LargeInt add(final LargeInt passedIn) 
    { 
     String stringOne = myArray.toString(); 
     String stringTwo = passedIn.myArray.toString(); 

     int r = Integer.parseInt(stringOne); 
     int e = Integer.parseInt(stringTwo); 
     int s = r + e; 
     return new LargeInt(""+s); 
    } 

    public void setValue(final String arrayString) 
    { 
     this.myArray = new int[arrayString.length()]; 
     for(int x = 0; x < myArray.length; x++) 
     { 
     this.myArray[x]=arrayString.charAt(x); 
     } 
    } 

    @Override 
    public int compareTo(LargeInt passedIn) 
    { 
     if(passedIn == null) 
     { 
     throw new RuntimeException("NullExceptionError"); 
     } 
     int ewu = 0; 
     int avs = 0; 

     if(this.myArray.length != passedIn.myArray.length) 
     { 
     return this.myArray.length - passedIn.myArray.length; 
     } 
     for(int i = 0; i < this.myArray.length -1; i++) 
     { 
     if(this.myArray[i] != passedIn.myArray[i]) 
     { 
      return this.myArray[i]-passedIn.myArray[i]; 
     } 
     } 
     return ewu-avs; 
    } 
    public int hashCode() 
    { 
     String p = ""; 
     for(int f = 0; f < this.myArray.length; f++) 
     { 
     p += myArray[f]; 
     } 

     return p.hashCode(); 
    } 

    public String getValue() 
    { 
     String h = ""; 
     for(int t = 0; t < this.myArray.length; t++) 
     { 
     h += myArray[t]; 
     } 
    return h; 
    } 

    @Override 
    public boolean equals(Object jbo) 
    { 
     if(jbo == null) 
     { 
     return false; 
     } 
     if(!(jbo instanceof LargeInt)) 
     { 
     return false; 
     } 
     LargeInt k =(LargeInt)jbo; 
     if(k.myArray.length != this.myArray.length) 
     { 
     return false; 
     } 
     for(int d = 0; d < this.myArray.length; d++) 
     { 
     if(k.myArray[d] != myArray[d]) 
     { 
      return false;   
     }  
     } 

     return true; 
    } 

    @Override 
    public String toString() 
    { 
     String c = ""; 
     for(int q = 0; q < this.myArray.length; q++) 
     { 
     c += myArray[q]; 
     } 
    return "The LargeInt is: " + c; 
    } 
} 

HW7Methods文件

import java.util.*; 
import java.io.*; 

public class HW7Methods 
{ 
    public static String readNum(Scanner kb) 
    { 
     String num = ""; 

     System.out.print("Enter Your Large Int: "); 

     num = kb.nextLine(); 

     return num; 
    } 
    public static void displayAscendingOrder(final LargeInt first, final LargeInt second, final LargeInt third) 
    { 
     String highestInt; 
     if(first.compareTo(second) >= 0 && first.compareTo(third) >= 0) 
     { 
     highestInt = first.getValue(); 
     } 
     else if(second.compareTo(first) >= 0 && second.compareTo(third) >= 0) 
     { 
     highestInt = second.getValue(); 
     } 
     else 
     { 
     highestInt = third.getValue(); 
     } 
     String middleInt; 
     if(first.compareTo(second) >= 0 && first.compareTo(third) <= 0) 
     { 
     middleInt = first.getValue(); 
     } 
     else if(second.compareTo(first) >= 0 && second.compareTo(third) <= 0) 
     { 
      middleInt = second.getValue(); 
     } 
     else 
     { 
     middleInt = third.getValue(); 
     } 
     String lowestInt; 
     if(first.compareTo(second) <= 0 && first.compareTo(third) <= 0) 
     { 
     lowestInt = first.getValue(); 
     } 
     else if(second.compareTo(first) <= 0 && second.compareTo(third) <= 0) 
     { 
     lowestInt = second.getValue(); 
     } 
     else 
     { 
     lowestInt = third.getValue(); 
     } 
     System.out.println("The LargeInts in order are: " + lowestInt + ", " + middleInt + ", " + highestInt); 

    } 
} 

MyUtil文件

package MyUtils; 
import java.io.*; 
import java.util.Scanner; 

public class MyUtil 
{ 
    public static int menu(Scanner kb) 
    { 
     int userChoice; 
     System.out.println("1) Print First Int"); 
     System.out.println("2) Print Second Int"); 
     System.out.println("3) Add Different Int"); 
     System.out.println("4) Check If Equal"); 
     System.out.println("5) Add Large Ints"); 
     System.out.println("6) Display In Ascending Order"); 
     System.out.println("7) Compare Ints"); 
     System.out.println("8) Quit"); 



     kb = new Scanner(System.in); 

     System.out.print("Please Select Your Choice: "); 
     userChoice = kb.nextInt(); 

     while(userChoice < 1 || userChoice > 8) 
     { 
     System.out.print("Invalid Menu Choice. Please Re-Enter: "); 
     userChoice = kb.nextInt(); 
     } 

     return userChoice; 
    } 
} 

當我去運行這段代碼,它提示我,因爲兩個大整數像它應該。但是,當我選擇選項5添加它們,這是我得到:

Exception in thread "main" java.lang.NumberFormatException: For input string: "[[email protected]" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:580) 
    at java.lang.Integer.parseInt(Integer.java:615) 
    at LargeInt.add(LargeInt.java:24) 
    at CSCD210HW7.main(CSCD210HW7.java:41) 

我從來沒有見過這種類型的錯誤之前。有人能告訴我發生了什麼事嗎?

+0

我覺得'add'方法的想法是,你應該添加兩個'LargeInt'數字數字。你想要做的是將它們都轉換回int,然後將它們相加 - 但這不是「大整數」類的要點。這是爲了存儲太大而不適合'int'數據類型的數字。所以我的猜測是你接近完成任務是錯誤的。 – 2014-11-22 01:52:39

+0

有趣的是如何類似這是:http://stackoverflow.com/questions/27072499/checking-if-values-in-array-are-different-java(我的意思是代碼) – Tom 2014-11-22 01:53:49

回答

3

對於輸入字符串:「[I @ 55f96302

這不是一個‘正確’的字符串,你正試圖在這裏解析

這是一個int[]看什麼,當你撥打喜歡toString()就可以了。

String stringOne = myArray.toString(); 

你爲什麼這樣做呢?那是什麼呢?

int r = Integer.parseInt(stringOne); 
int e = Integer.parseInt(stringTwo); 
int s = r + e; 

從它的外觀來看,你試圖通過以某種方式將它們存儲在一個整數數組中來處理「大」整數類。沒關係,BigInteger也可以這樣工作(或多或少),但是您不能僅僅通過嘗試轉換回int來進行計算(即使您正確地進行字符串解析,所有這些數字都太大,以至於不能處理算術int) 。

+0

好吧,那麼你會如何去解決錯誤? – dhjdsh 2014-11-22 05:33:35

+0

'int'不能處理這些大數字。你需要以某種方式實現自己的添加。這真的是一項艱鉅的任務。也許看看'BigInteger'的源代碼(我假設這是一個學校任務,在現實生活中,你可以直接使用'BigInteger')。 – Thilo 2014-11-24 01:08:15

相關問題