aspose file tools*
The moose likes Beginning Java and the fly likes java temperature converter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java temperature converter" Watch "java temperature converter" New topic
Author

java temperature converter

Chris Carlson
Greenhorn

Joined: Feb 11, 2010
Posts: 5
I am having a problem with this program and I can't see what it is. I am entering a temp and a letter, C or c, F or f, and then converting based on the letter entered. It will not go past the first if statement and no matter what letter you enter, it always does the first if and then quits. I am a college student looking for help. Any suggestions?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40


Well, you did this....



And your first condition is this...



Does it really matter what the user entered? Of course, the first condition is going to be true. You assigned them to the same thing only a few lines earlier.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Chris,

You've also used input.next() twice to get the temperature type input, when the user will only ever input it once.

Also, when I fixed this and the problem that Henry is alluding to, I still couldn't get the user input to '==' the temperatureType string. This is, I think, because the input statement also records the return key input. I wasn't sure how to fix this apart from to just compare the first char of the user input rather than the whole string. Someone else might have a better idea.

Hope that helps

Joe


OCPJP 85%
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Never use == for comparing Strings, use .equals. == checks for reference equality instead of content (String) equality.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Sorry Chris. What I wrote about the input.next() method also recording the return character is a load of rubbish. Rob has pointed out the reason it wasn't working. Thankfully someone knows what they're talking about.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Welcome to the Ranch Since you are new, I have corrected your code tags and you can see how much better the post looks.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

you probably want to learn about "normalizing" your data. In this case, what that means is to sort of force your input data into the form you want it, so that it is consistent. You can't MAKE the user always input a capital 'C'.

but, one you get their input, you can convert whatever they gave you into all caps. That makes your testing easier.

For example, if i was asking for a first name, and I wanted to do something special if they input the name 'fred' with any capitalization, i'd do this:



by doing this, the user could input "fred", "Fred", "FRED" or even "fReD" and it doesn't matter...I only need one check. I don't have to think of EVERY possible way they could write it and test for each.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

fred rosenberger wrote:you probably want to learn about "normalizing" your data. In this case, what that means is to sort of force your input data into the form you want it, so that it is consistent. You can't MAKE the user always input a capital 'C'.

but, one you get their input, you can convert whatever they gave you into all caps. That makes your testing easier.



I agree, but some people prefer to use the method that compares strings, ignoring the case.

Maybe Chris should check out the API for String....

And the reason it's not working.... well maybe you should look into this thread....


When you do things right, people won't be sure you've done anything at all.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Janeice DelVecchio wrote:I agree, but some people prefer to use the method that compares strings, ignoring the case.

Which is fine when all you have is a text String, but normalization methods can be expanded for when users input things like "&" vs "and", or "2nd" vs "second", etc.
 
 
subject: java temperature converter