• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic