gpt4 book ai didi

java - 数字格式异常 : For input string: "[memorylocation" java

转载 作者:行者123 更新时间:2023-12-01 22:34:34 28 4
gpt4 key购买 nike

我正在做一项作业,其目标之一是添加两个大整数。这是我的代码,分为四个文件。

我们无法更改的主要内容:

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: "[I@55f96302"
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)

我以前从未见过这种类型的错误。谁能告诉我发生了什么事吗?

最佳答案

For input string: "[I@55f96302

这不是您要在此处解析的“正确”字符串。

这就是调用 toString()int[] 的样子。

String stringOne = myArray.toString();

你为什么要这么做?那应该做什么?

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

从表面上看,您尝试使用 LargeInt 类通过某种方式将“大”整数存储在整数数组中来处理它们。没关系,BigInteger 也可以这样工作(或多或少),但是你不能仅仅通过尝试转换回 int 来进行计算(毕竟这些数字对于计算来说太大了)即使您正确地进行了字符串解析,也要处理 int 算术)。

关于java - 数字格式异常 : For input string: "[memorylocation" java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27073146/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com