• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem: calculator not working

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I was messing with calculator problem in introduction to java programming text.
When I have string arrays in A class and call the exercise class to have string variables to convert to char, everything works fine.
So i decided to try passing char arrays to exercise class thinking that it may be easier to pass char variables without conversion.
It works except that the result i am getting is all wrong. For example 1 + 3 comes out as 100.

Any idea what i am doing wrong??

Thanks in advance.


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The char '1' is not the number one, it is a numeric character code used to represent the character '1'. You still need to convert from char to int.
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:The char '1' is not the number one, it is a numeric character code used to represent the character '1'. You still need to convert from char to int.



Thanks Steve. hmm.. then when I convert from string to char and pass on string arrays, why does it give me a correct answer?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dunno, show the code
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go.



 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just checked the character codes for digits (http://www.unicode.org/charts/PDF/U0000.pdf). '1' is 0x0031, and '3' is 0x0033. Add them and you get 0x0064 which in decimal (those are hex numbers) comes out to be 100 - which is what your calculator is getting.
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. nvm. I did convert from string to int.

Thank you.
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more question,

is line 11 (args[1].charAt(0)) from exercise class necessary?
what is the reason for converting to char instead of leaving it as string?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Albert Park wrote:one more question,

is line 11 (args[1].charAt(0)) from exercise class necessary?
what is the reason for converting to char instead of leaving it as string?



I don't think it is necessary any more - try running without it. The reason is that until Java 7 (I think) you couldn't run a switch on a String, only on enums and on primitives - and chars are primitives. I may be wrong about Java 7 tho, so give it a try and see if it works now without the conversion.
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:

Albert Park wrote:one more question,

is line 11 (args[1].charAt(0)) from exercise class necessary?
what is the reason for converting to char instead of leaving it as string?



I don't think it is necessary any more - try running without it. The reason is that until Java 7 (I think) you couldn't run a switch on a String, only on enums and on primitives - and chars are primitives. I may be wrong about Java 7 tho, so give it a try and see if it works now without the conversion.



oh ic. yea, like you said it does work with String. Makes more sense now. Thank you.
 
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic