Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

breaks in converting String to Integer

 
Ranch Hand
Posts: 40
  • 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=)


 
Ranch Hand
Posts: 198
Oracle Java Linux
  • 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: 198
Oracle Java Linux
  • Likes 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
  • 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=///
 
Marshal
Posts: 64666
225
  • Likes 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.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Likes 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: 198
Oracle Java Linux
  • 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
  • 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
Marshal
Posts: 64666
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!