2014-11-22 65 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

相关问题