0

I'm working on a project currently where I have a String array of 8 values that number but are stored as strings. String[3] and String[7] are letters stored as strings that I need to convert to their ASCII values, but I can't seem to do that in java. I keep getting an error saying that I cannot convert a String type to int type, and I don't know any other way to get these string letters to their ASCII values. Here is the code I have so far...

String stringInfo [] = input.split(",");
    int info [] = new int [8];
    int x = 0;
    while (x<stringInfo.length) {
        info[x] = Integer.parseInt(stringInfo[x]);
        System.out.println(info[x]);
        x++;
    }

so within that array, those two values need to be turned into ASCII but that code keeps getting errors and I don't know how to fix it. How would I do this?

3
  • 1
    Please show us some sample input and output. A picture would be worth a thousand words here. Commented Jan 25, 2018 at 4:51
  • 1
    can you show the input / output data and the error you have received? Commented Jan 25, 2018 at 4:58
  • elements in an array of Strings to their ascii values in java.I think you want the integer data no ascii code ! Commented Jan 25, 2018 at 15:02

3 Answers 3

1

The best way to do this would be to first convert your string to a character, and then cast your character as an int. I know this sounds like a lot, but it is actually only one line of code.

int ascii = (int) mystring.charAt(0);

The reason this works is that characters (char) are a primary type in Java, and they essentially are just bit, which is why you can actually compare chars to each other using == rather than .equals.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a function that use ascii code to convert string of unsigned number to an integer:

static int stringToNumber(String input) {
    int output = 0;
    for (int x = 0; x < input.length(); x++) {
        output = output * 10 + (input.charAt(x) - '0');

    }
    return output;
}

And your code

String stringInfo [] = input.split(",");
    int info [] = new int [8];
    int x = 0;
    while (x<stringInfo.length) {
        info[x] = stringToNumber(stringInfo[x]);
        System.out.println(info[x]);
        x++;
}

But if you don't need ascii code or your number are signed number java made it easier you can only use this method to convert string to number

Integer result = Integer.valueOf(stringInfo[x]);

And your code:

String stringInfo [] = input.split(",");
    int info [] = new int [8];
    int x = 0;
    while (x<stringInfo.length) {
        info[x] = Integer.valueOf(stringInfo[x]);
        System.out.println(info[x]);
        x++;
}

2 Comments

the function worked perfectly, thank you! can you explain why you have to do the - '0' though?
Because in ASCII code number are continuous. after each other first was zero '0', after zero '1' ..... '9' when want to get the value of ASCII character only can do it with - '0' that mean zero is now not the zero in ascii it's be zero and '1' = '0' + 1 when minus by '0' remind 1 and other ...
0

You can try-

String stringInfo [] = input.split(",");
int info [] = new int [8];

int x = 0;
    while (x<stringInfo.length) {
        info[x] = stringInfo.charAt(x);
        System.out.println(info[x]);
        x++;
    }

stringInfo.charAt(i) will give the Ascii value at index(i) of stringInfo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.