aspose file tools*
The moose likes Java in General and the fly likes breaks in converting String to Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "breaks in converting String to Integer" Watch "breaks in converting String to Integer" New topic
Author

breaks in converting String to Integer

Anissa Pary
Ranch Hand

Joined: Jan 02, 2012
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

Joined: Aug 22, 2008
Posts: 193

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.


Do not wait to strike till the iron is hot; but make it hot by striking....
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 193

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

Joined: Jan 02, 2012
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

Joined: Oct 13, 2005
Posts: 40029
    
  28
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

Joined: Dec 16, 2010
Posts: 1065
    
  13

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:


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 193

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

Joined: Jan 02, 2012
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

Joined: Oct 13, 2005
Posts: 40029
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: breaks in converting String to Integer