*
The moose likes Beginning Java and the fly likes problem: calculator not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem: calculator not working" Watch "problem: calculator not working" New topic
Author

problem: calculator not working

Albert Park
Greenhorn

Joined: Feb 01, 2012
Posts: 28
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

Joined: Jan 28, 2003
Posts: 3969
    
  17

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.


Steve
Albert Park
Greenhorn

Joined: Feb 01, 2012
Posts: 28
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

Joined: Jan 28, 2003
Posts: 3969
    
  17

Dunno, show the code
Albert Park
Greenhorn

Joined: Feb 01, 2012
Posts: 28
Here you go.



Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3969
    
  17

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

Joined: Feb 01, 2012
Posts: 28
ok. nvm. I did convert from string to int.

Thank you.
Albert Park
Greenhorn

Joined: Feb 01, 2012
Posts: 28
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

Joined: Jan 28, 2003
Posts: 3969
    
  17

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

Joined: Feb 01, 2012
Posts: 28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem: calculator not working
 
Similar Threads
Problem in switch statement
using string tokenizer to retrieve rationals
For the Love of Loops!
Help! Command-line arguments
String arguments