Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# breaks in converting String to Integer

Anissa Pary
Ranch Hand
Posts: 40

Can anyone please help when I was debugging that it does not go after I wanted to make an Integer from String and it breaks here I cannot see the problem. The test is below

The output is :

22+33 = 55
+33 = 55
number is:

However I wanted it to be number is: 22. Thank you in advance=)

Manoj Kumar Jain
Ranch Hand
Posts: 198
How this code is running. I am even not able to compile the code.

and also it gives the java.lang.ClassCastException when I correct and run the code.

Manoj Kumar Jain
Ranch Hand
Posts: 198
• 1
Look how you initialized "numbers" and what values are you placing value in it ??

Can you type cast the result of numbers.get(d) to an String object ?

Anissa Pary
Ranch Hand
Posts: 40
Manoj Kumar Jain wrote:Look how you initialized "numbers" and what values are you placing value in it ??

Can you type cast the result of numbers.get(d) to an String object ?

Ok, thanks get it.=) It is because of char=///

Campbell Ritchie
Sheriff
Posts: 50749
83
• 1
There is a much simpler way to find out whether a char represents a digit or 0.
And don’t write if (...) return true; else return false;Yes, you can do arithmetic with chars, because, contrary to popular belief they are not letters. They are numbers. '0' is stored as (I think) 0x30 = 48decimal. '0' to '9' are a consecutive sequence, so you can use that trick.
There is also a method in the Character class which does the same thing.

J. Kevin Robbins
Bartender
Posts: 1801
28
• 1
Campbell Ritchie wrote:There is a much simpler way to find out whether a char represents a digit or 0.
And don’t write if (...) return true; else return false;Yes, you can do arithmetic with chars, because, contrary to popular belief they are not letters. They are numbers. '0' is stored as (I think) 0x30 = 48decimal. '0' to '9' are a consecutive sequence, so you can use that trick.
There is also a method in the Character class which does the same thing.

That's a neat trick. I couldn't resist playing around with this one, too, but I went another direction.

Now the test becomes:

Manoj Kumar Jain
Ranch Hand
Posts: 198
yes, this trick is good if we need to look for a specific characters instead of consecutive digits/chars. This provide more flexibility to check for the characters appearing in the String/Input.

Anissa Pary
Ranch Hand
Posts: 40
Campbell Ritchie wrote:There is a much simpler way to find out whether a char represents a digit or 0.
And don’t write if (...) return true; else return false;Yes, you can do arithmetic with chars, because, contrary to popular belief they are not letters. They are numbers. '0' is stored as (I think) 0x30 = 48decimal. '0' to '9' are a consecutive sequence, so you can use that trick.
There is also a method in the Character class which does the same thing.

Thank you! I did not know that char are not like String)))

Campbell Ritchie
Sheriff
Posts: 50749
83
You’re welcome