0

Possible Duplicate:
Java char array to int

I have the following code, where stackCells is an ArrayList object:

int[] stackCell = {0, 0};

if (!stackCells.isEmpty())  {
    String stackCellString = stackCells.get(0);
    stackCell[0] = stackCellString.charAt(0);
    stackCell[1] = stackCellString.charAt(1);
}

So the problem I am encountering is that stackCell is not interpreting the character as an int. For example, the value of stackCellString should be "88". However, when I use chartAt(0) and charAt(1), I get an int value of 56. If I use those same calls inside a System.out.println(stackCellString.charAt(0) + stackCellString.charAt(1)) command, I get the correct result (i.e. "88").

5
  • what is stackCellString Commented Jan 2, 2013 at 3:45
  • @Woot4Moo I have it written in the paragraph. Commented Jan 2, 2013 at 3:46
  • @KarthikT But I thought a char can be used as an int without casting. I tried casting too, with no luck. Commented Jan 2, 2013 at 3:47
  • why are you using an int array? if you are trying to store chars? Commented Jan 2, 2013 at 3:51
  • @SeanF I need to store the index of a multidimensional array. Commented Jan 2, 2013 at 4:02

3 Answers 3

2

It's interpreting the character as an int in the following way:

56 is the ASCII code for the character '8'.

If you want to grab the numeric digits from the string, you will need to use something like Integer.parseInt() as one of the comments mentioned.

A more efficient solution might be to do some math based on the ASCII code. If you know all of the characters are decimal digits, you could do (code - 48) to get the digit (where code is the ASCII code). For instance, (56 - 48) = 8, which is what you want here.

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

3 Comments

doing parseInt is very expensive.
True. Alternatively, you could compute the digit based on the ASCII code.
Thanks a lot. ft1 answered with that idea first, so I'm giving this one to him/her.
1

56 is the ASCII code of the digit 8. To get what you want, you could do this:

stackCell[0] = stackCellString.charAt(0) - '0';
stackCell[1] = stackCellString.charAt(1) - '0';

3 Comments

Perfect answer. Nothing simpler.
The simpler solution is to not pass around strings knowing you have to convert them later.
@Woot4Moo Sure, but I have no choice. The code given does not give the context of my program, so I can understand why you may find my approach confusing.
0
String stackCellString="88";
        stackCell[0] = stackCellString.charAt(0);
        stackCell[1] = stackCellString.charAt(1);
        System.out.println(stackCell[0]);
        System.out.println(stackCell[1]);
        System.out.println(stackCell[0]+stackCell[1]);
        System.out.println(stackCellString.charAt(0) + stackCellString.charAt(1));

output:

56
56
112
112

You are not returning the charAt as its character representation.

If you want it to return "88" you need to do this:

stackCellString.subString(0,1) + stackCellString.subString(1,2);  

Another work around is this:

"" + stackCellString.charAt(0) + stackCellString.charAt(1);

6 Comments

Thanks. But what am I returning it as my way, if not the character representation?
@capcom you are returning the short value of the character.
Is there an 'inexpensive' way to get what I want? I need the 8s as integer values, not substrings.
@capcom the least expensive way is to not convert from a String to an Int. Make your ArrayList one of Integers. If that is not practical then you will need to eat the conversion cost of String to Integer
Check out ft1's solution above. It works great, and is anything but expensive.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.