• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java temperature converter

 
Chris Carlson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == for comparing Strings, use .equals. == checks for reference equality instead of content (String) equality.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic