File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 191

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: 191

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: 36513
    
  16
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
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

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:


"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

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: 36513
    
  16
You’re welcome
 
jQuery in Action, 2nd edition
 
subject: breaks in converting String to Integer
 
Similar Threads
String to Integer conversion - why this error?
HELP~~
Help with english to morse code program, I have no idea what I'm doing
Two Dimensional Arrays
Please help in this selection sort