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

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

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

问题

public class ChangeCase {
   String stringToCheck;

  public int convertToIntPlusTen() {
        String asciiValue = "";  // 创建用于存储转换后字符串的变量
        final char[] chars = stringToCheck.toCharArray();  // 将原始字符串拆分为字符数组
        for (int i = 0; i < chars.length; i++) {
            asciiValue += String.valueOf((int) chars[i]);  // 计算每个字符的ASCII值并追加到新变量
        }
        int asciiInt = Integer.parseInt(asciiValue);  // 将包含数字字符的字符串转换为整数
        asciiInt += 10;  // 对结果整数加上10
        return asciiInt;  // 返回计算结果
    }
}

public class AppDriver {
    public static void main(String[] args) {
       ChangeCase changeCase = new ChangeCase();
        changeCase.stringToCheck = "Foxes";  // 要处理的字符串
        changeCase.convertToIntPlusTen();  // 调用方法进行转换和计算
    }
}

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

英文:

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.

public class ChangeCase {
   String stringToCheck;

  public int convertToIntPlusTen() {
        // Create new field for the converted string
        String asciiValue = &quot;&quot;;
        // 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 &lt; 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(&quot;&quot;);
        sbf.append(asciiInt);

        return asciiInt;
    }
}

public class AppDriver {
    public static void main(String[] args) {
       ChangeCase changeCase = new ChangeCase();
        changeCase.stringToCheck = &quot;Foxes&quot;;
        changeCase.convertToIntPlusTen();
    }
}

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:

Exception in thread &quot;main&quot; java.lang.NumberFormatException: For input string: &quot;70111120101115&quot;
	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)

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

因此,你的类应该是:

public class ChangeCase {
    String stringToCheck;
    
    public long convertToIntPlusTen() {
        // 创建一个新字段来存储转换后的字符串
        String asciiValue = "";
        // 将原始字符串分解为字符数组
        final char[] chars = stringToCheck.toCharArray();
        // 找到每个字符的ASCII值并添加到新字段中
        for (int i = 0; i < chars.length; i++) {
            asciiValue += String.valueOf((int) chars[i]);
        }
        // 将数字字符的字符串转换为长整型
        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() {
        // 创建一个新字段来存储转换后的字符串
        String asciiValue = "";
        // 将原始字符串分解为字符数组
        final char[] chars = stringToCheck.toCharArray();
        // 找到每个字符的ASCII值并添加到新字段中
        for (int i = 0; i < chars.length; i++) {
            asciiValue += String.valueOf((int) chars[i]);
        }
        // 将数字字符的字符串转换为长整型
        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();
    }
}
英文:

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:

  public long convertToIntPlusTen() {
        // Create new field for the converted string
        String asciiValue = &quot;&quot;;
        // 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 &lt; 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(&quot;&quot;);
        sbf.append(asciiInt);

        return asciiInt;
    }

So your final code should be:

public class ChangeCase {
   String stringToCheck;

  public long convertToIntPlusTen() {
        // Create new field for the converted string
        String asciiValue = &quot;&quot;;
        // 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 &lt; 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(&quot;&quot;);
        sbf.append(asciiInt);

        return asciiInt;
    }
}

public class AppDriver {
    public static void main(String[] args) {
       ChangeCase changeCase = new ChangeCase();
        changeCase.stringToCheck = &quot;Foxes&quot;;
        changeCase.convertToIntPlusTen();
    }
}

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:

确定