Skip to content
Advertisement

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

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 = "";
        // 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();
    }
}

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 "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)

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.

Advertisement

Answer

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 = "";
        // 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;
    }

So your final code should be:

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();
    }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement