问题描述
我在试图查找如何将String转换为int的每个地方,这些示例都使用了数字字符串,即它们显示了如何将“ 123”更改为123。这根本不是我想要做的。我的字符串是字母。我之所以知道这一点,是因为前两种方法需要我首先检查它是否包含大写字符,然后将其转换为所有大写字符。我成功做到了这两个。现在,第三个方法要求将其转换为int并对其执行算术函数,现在我陷入了困境。我尝试使用.valueOf(),即字符的ascii数值,但始终会引发错误。
public class ChangeCase {
String stringtocheck;
public int convertToIntPlusTen() {
// Create new field for the converted string
String asciiValue = "";
// Break original string down to char array
final char[] chars = stringtocheck.tochararray();
// Find the ascii value of each character and add it to the new field
for (int i = 0; i < chars.length; i++) {
asciiValue += String.valueOf((int) chars[i]);
}
// Convert string of numeric characters to int
int asciiInt = Integer.parseInt(asciiValue);
// Add ten to the resulting int
asciiInt += asciiInt + 10;
StringBuilder sbf
= new StringBuilder("");
sbf.append(asciiInt);
return asciiInt;
}
}
public class AppDriver {
public static void main(String[] args) {
ChangeCase changeCase = new ChangeCase();
changeCase.stringtocheck = "Foxes";
changeCase.convertToIntPlusTen();
}
}
现在,由于字符的ascii值是'F'= 070,'o'= 111,'x'= 120,'e'= 101和's'= 115,那么我希望它会产生数字字符串“ 070111120101115”,它将变成int070111120101115。加十将使其成为070111120101125,这是预期的输出。
相反,我得到了:
Exception in thread "main" java.lang.NumberFormatException: For input string: "70111120101115"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
at java.lang.Integer.parseInt(Integer.java:615)
at mainpackage.SubChangeCase.convertToIntPlusTen(SubChangeCase.java:45)
at mainpackage.AppDriver.main(AppDriver.java:133)
我在想,这可能是逻辑错误,而不是操作错误,也就是说,我可能一开始就错误地解决了这个问题。因为我看到我的堆栈跟踪确实具有预期的输入字符串。不幸的是,由于互联网世界中几乎所有的代码示例都是关于转换数字字符串的,所以我还没有找到任何帮助的方法。
解决方法
70111120101115
对于整数而言太大。您必须将其存储在 long
您还输入了错误-实例化了错误的课程。它是 ChangeCase ,而不是 SubChangeCase
因此,您的课程应该是:
public long convertToIntPlusTen() {
// Create new field for the converted string
String asciiValue = "";
// Break original string down to char array
final char[] chars = stringToCheck.toCharArray();
// Find the ascii value of each character and add it to the new field
for (int i = 0; i < chars.length; i++) {
asciiValue += String.valueOf((int) chars[i]);
}
// Convert string of numeric characters to int
long asciiInt = Long.parseLong(asciiValue);
asciiInt += asciiInt + 10;
StringBuilder sbf
= new StringBuilder("");
sbf.append(asciiInt);
return asciiInt;
}
因此您的最终代码应为:
public class ChangeCase {
String stringToCheck;
public long convertToIntPlusTen() {
// Create new field for the converted string
String asciiValue = "";
// Break original string down to char array
final char[] chars = stringToCheck.toCharArray();
// Find the ascii value of each character and add it to the new field
for (int i = 0; i < chars.length; i++) {
asciiValue += String.valueOf((int) chars[i]);
}
// Convert string of numeric characters to int
long asciiInt = Long.parseLong(asciiValue);
asciiInt += asciiInt + 10;
StringBuilder sbf
= new StringBuilder("");
sbf.append(asciiInt);
return asciiInt;
}
}
public class AppDriver {
public static void main(String[] args) {
ChangeCase changeCase = new ChangeCase();
changeCase.stringToCheck = "Foxes";
changeCase.convertToIntPlusTen();
}
}