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 Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem: calculator not working

 
Albert Park
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • 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.


 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dunno, show the code
 
Albert Park
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go.



 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. nvm. I did convert from string to int.

Thank you.
 
Albert Park
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic