File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

breaks in converting String to Integer

 
Anissa Pary
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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: 195
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 195
Java Linux Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47292
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1607
15
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 195
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic