gpt4 book ai didi

输入字符串的 java.lang.NumberFormatException

转载 作者:行者123 更新时间:2023-12-02 05:10:42 24 4
gpt4 key购买 nike

我正在创建一个程序,它将给定的输入字符串转换为数字,以便对输入进行编码。但一旦输入字符串变得太长,我就会遇到 NumberFormatException 。我不知道如何解决这个问题。

请注意,我必须从给定的字符串输入中获取子字符串,将它们转换为 numericValues,然后获取这两个字符串的总和作为答案。

代码:

public class Puzzle {


private static char[] letters = {'a','b','c','d','e','f','g','h','i', 'j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z'};
private static String input;
private static String delimiters = "\\s+|\\+|//+|=";
public static void main(String[]args)
{
input = "youuu + are = gay"; //as soon as the substrings before = sign are
//longer than 5 characters the exception occurs

System.out.println(putValues(input));
}

//method to put numeric values for substring from input
@SuppressWarnings("static-access")
public static long putValues(String input)
{

Integer count = 0;
long answer = 0;
String first="";
String second = "";
StringBuffer sb = new StringBuffer(input);
int wordCounter = Countwords();
String[] words = countLetters();
System.out.println(input);

if(input.isEmpty())
{
System.out.println("Sisestage mingi s6na");
}
if(wordCounter == -1 ||countLetters().length < 1){

return -1;
}
for(Character s : input.toCharArray())
{

for(Character c : letters)
{
if(s.equals(c))
{
count = c.getNumericValue(c);

System.out.print(s.toUpperCase(s) +"="+ count + ", ");

}

}
if(words[0].contains(s.toString()))
{
count = count - 1;
count = s.getNumericValue(s);
//System.out.println(count);
first += count.toString();

}
if(words[3].contains(s.toString())){

count = s.getNumericValue(s);
second += count.toString();
}

}
try {

answer = Integer.parseInt(first) + Integer.parseInt(second);
} catch(NumberFormatException ex)
{
System.out.println(ex);
}

System.out.println("\n" + first + " + " + second + " = " + answer);


return answer;

}
public static int Countwords()
{
String[] countWords = input.split(" ");
int counter = countWords.length - 2;
if(counter == 0) {
System.out.println("Sisend puudu!");
return -1;
}
if(counter > 1 && counter < 3) {
System.out.println("3 sõna peab olema");
return -1;
}
if(counter > 3) {
System.out.println("3 sõna max!");
return -1;
}
return counter;
}

//method which splits input String and returns it as an Array so i can put numeric values after in the
//putValue method
public static String[] countLetters()
{

int counter = 0;
String[] words = input.split(delimiters);
for(int i = 0; i < words.length;i++) {

counter = words[i].length();

if(words[i].length() > 18) {
System.out.println("Yhe s6na maksimaalne pikkus on 18 t2hem2rki ");
}
}
return words;
}

最佳答案

Java 中的整数(与许多语言一样)受到最小值和最大值的限制。

有关此内容的更多信息可以在这里找到:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

您可以在 catch block 中给出有意义的错误

You did not enter a valid 32-bit Integer value.

或者您可以切换到类似 BigDecimal 的东西,它可以容纳更大的值:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

(注意:BigDecimal 的工作方式与普通 int 非常不同,因此请明智地阅读文档,如有必要,请参阅 Google 的示例)

编辑:如果您愿意,您也可以将其解析为 Long:Long.parseLong(INPUT, 10);。这样您就可以将限制扩展到 64 位。

关于输入字符串的 java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363365/

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