如何将字母字符串转换为整数并进行算术运算?

huangapple go评论94阅读模式
英文:

How do I convert an alphabetic string to int and do arithmetic on it?

问题

  1. public class ChangeCase {
  2. String stringToCheck;
  3. public int convertToIntPlusTen() {
  4. String asciiValue = ""; // 创建用于存储转换后字符串的变量
  5. final char[] chars = stringToCheck.toCharArray(); // 将原始字符串拆分为字符数组
  6. for (int i = 0; i < chars.length; i++) {
  7. asciiValue += String.valueOf((int) chars[i]); // 计算每个字符的ASCII值并追加到新变量
  8. }
  9. int asciiInt = Integer.parseInt(asciiValue); // 将包含数字字符的字符串转换为整数
  10. asciiInt += 10; // 对结果整数加上10
  11. return asciiInt; // 返回计算结果
  12. }
  13. }
  14. public class AppDriver {
  15. public static void main(String[] args) {
  16. ChangeCase changeCase = new ChangeCase();
  17. changeCase.stringToCheck = "Foxes"; // 要处理的字符串
  18. changeCase.convertToIntPlusTen(); // 调用方法进行转换和计算
  19. }
  20. }

注:代码中使用了一些注释来解释代码的功能。如果你需要更多关于代码的解释,可以随时提问。

英文:

Everywhere I look trying to find out how to convert a String to an int, the examples use a numeric string, i.e. they show how to change "123" into 123. That is not at all what I am trying to do. My string is alphabetic. I know this because the two previous methods required me, first, to check whether it contained uppercase characters, and then, convert it to all uppercase characters. I succeeded in doing both of these. Now the third method calls for converting it to an int, and performing an arithmetic function on it, and now I am stuck. I tried using .valueOf(), that is, the ascii numeric values of the characters, but it keeps throwing errors.

  1. public class ChangeCase {
  2. String stringToCheck;
  3. public int convertToIntPlusTen() {
  4. // Create new field for the converted string
  5. String asciiValue = &quot;&quot;;
  6. // Break original string down to char array
  7. final char[] chars = stringToCheck.toCharArray();
  8. // Find the ascii value of each character and add it to the new field
  9. for (int i = 0; i &lt; chars.length; i++) {
  10. asciiValue += String.valueOf((int) chars[i]);
  11. }
  12. // Convert string of numeric characters to int
  13. int asciiInt = Integer.parseInt(asciiValue);
  14. // Add ten to the resulting int
  15. asciiInt += asciiInt + 10;
  16. StringBuilder sbf
  17. = new StringBuilder(&quot;&quot;);
  18. sbf.append(asciiInt);
  19. return asciiInt;
  20. }
  21. }
  22. public class AppDriver {
  23. public static void main(String[] args) {
  24. ChangeCase changeCase = new ChangeCase();
  25. changeCase.stringToCheck = &quot;Foxes&quot;;
  26. changeCase.convertToIntPlusTen();
  27. }
  28. }

Now since the ascii values of the characters are 'F' = 070, 'o' = 111, 'x' = 120, 'e' = 101, and 's' = 115, then I expected it to produce the numeric string "070111120101115," which would then become the int 070111120101115. Adding ten would make it 070111120101125, which is the expected output.

Instead I get:

  1. Exception in thread &quot;main&quot; java.lang.NumberFormatException: For input string: &quot;70111120101115&quot;
  2. at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  3. at java.lang.Integer.parseInt(Integer.java:583)
  4. at java.lang.Integer.parseInt(Integer.java:615)
  5. at mainpackage.SubChangeCase.convertToIntPlusTen(SubChangeCase.java:45)
  6. at mainpackage.AppDriver.main(AppDriver.java:133)

I'm thinking that maybe this is more of a logical error than an operational one, i.e. I may have approached the problem incorrectly in the first place. Because I see my stack trace does have the expected input string. Unfortunately, since almost every code example out there in internet world is about converting numeric strings, I have not found anything to help with this.

答案1

得分: 1

70111120101115对于一个 整数 来说太大了。你需要将它存储在一个 长整型 中。

你还犯了一个拼写错误 - 你实例化了错误的类。应该是 ChangeCase,而不是 SubChangeCase

因此,你的类应该是:

  1. public class ChangeCase {
  2. String stringToCheck;
  3. public long convertToIntPlusTen() {
  4. // 创建一个新字段来存储转换后的字符串
  5. String asciiValue = "";
  6. // 将原始字符串分解为字符数组
  7. final char[] chars = stringToCheck.toCharArray();
  8. // 找到每个字符的ASCII值并添加到新字段中
  9. for (int i = 0; i < chars.length; i++) {
  10. asciiValue += String.valueOf((int) chars[i]);
  11. }
  12. // 将数字字符的字符串转换为长整型
  13. long asciiInt = Long.parseLong(asciiValue);
  14. asciiInt += asciiInt + 10;
  15. StringBuilder sbf = new StringBuilder("");
  16. sbf.append(asciiInt);
  17. return asciiInt;
  18. }
  19. }

所以你的最终代码应该是:

  1. public class ChangeCase {
  2. String stringToCheck;
  3. public long convertToIntPlusTen() {
  4. // 创建一个新字段来存储转换后的字符串
  5. String asciiValue = "";
  6. // 将原始字符串分解为字符数组
  7. final char[] chars = stringToCheck.toCharArray();
  8. // 找到每个字符的ASCII值并添加到新字段中
  9. for (int i = 0; i < chars.length; i++) {
  10. asciiValue += String.valueOf((int) chars[i]);
  11. }
  12. // 将数字字符的字符串转换为长整型
  13. long asciiInt = Long.parseLong(asciiValue);
  14. asciiInt += asciiInt + 10;
  15. StringBuilder sbf = new StringBuilder("");
  16. sbf.append(asciiInt);
  17. return asciiInt;
  18. }
  19. }
  20. public class AppDriver {
  21. public static void main(String[] args) {
  22. ChangeCase changeCase = new ChangeCase();
  23. changeCase.stringToCheck = "Foxes";
  24. changeCase.convertToIntPlusTen();
  25. }
  26. }
英文:

70111120101115 is too big for an integer. You have to store it in a long

You also made a typo - you instantiated the wrong class. It's ChangeCase, not SubChangeCase

Therefore, your class should be:

  1. public long convertToIntPlusTen() {
  2. // Create new field for the converted string
  3. String asciiValue = &quot;&quot;;
  4. // Break original string down to char array
  5. final char[] chars = stringToCheck.toCharArray();
  6. // Find the ascii value of each character and add it to the new field
  7. for (int i = 0; i &lt; chars.length; i++) {
  8. asciiValue += String.valueOf((int) chars[i]);
  9. }
  10. // Convert string of numeric characters to int
  11. long asciiInt = Long.parseLong(asciiValue);
  12. asciiInt += asciiInt + 10;
  13. StringBuilder sbf
  14. = new StringBuilder(&quot;&quot;);
  15. sbf.append(asciiInt);
  16. return asciiInt;
  17. }

So your final code should be:

  1. public class ChangeCase {
  2. String stringToCheck;
  3. public long convertToIntPlusTen() {
  4. // Create new field for the converted string
  5. String asciiValue = &quot;&quot;;
  6. // Break original string down to char array
  7. final char[] chars = stringToCheck.toCharArray();
  8. // Find the ascii value of each character and add it to the new field
  9. for (int i = 0; i &lt; chars.length; i++) {
  10. asciiValue += String.valueOf((int) chars[i]);
  11. }
  12. // Convert string of numeric characters to int
  13. long asciiInt = Long.parseLong(asciiValue);
  14. asciiInt += asciiInt + 10;
  15. StringBuilder sbf
  16. = new StringBuilder(&quot;&quot;);
  17. sbf.append(asciiInt);
  18. return asciiInt;
  19. }
  20. }
  21. public class AppDriver {
  22. public static void main(String[] args) {
  23. ChangeCase changeCase = new ChangeCase();
  24. changeCase.stringToCheck = &quot;Foxes&quot;;
  25. changeCase.convertToIntPlusTen();
  26. }
  27. }

huangapple
  • 本文由 发表于 2020年10月7日 06:50:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64234831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定