• Post Reply Bookmark Topic Watch Topic
  • New Topic

input keybord  RSS feed

 
Bruno Van Hemel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I'am a java beginner. I'am try to program a temparture converter. My problem is if a start the program  and type first another letter (not c or f) then everthing is working fine. After that i can choose c or f.
but if i start the program type direct c the program close. Can someone help me  or give me a clue where i went worng.
Sorry for my bad English but language is Dutch.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Look closely at line 16; what will that return if you enter "C" or if you enter "F" or if you enter "D"? I think that may help explain the problem.
Don't use float arithmetic for your temperatures. Don't use float arithmetic for anything (unless some other code specifically needs a float). Use doubles. Don't use integer arithmetic for those temperatures because there is a risk of getting 0.
 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, next() returns a string. The first call to this is ok, but a subsequent call will return an empty string. you should try nextLine().
 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It think you mean


edit: This while loop should be if they don't enter 'c' or 'f', in which case you should re-prompt. But you have get temperature inside this while loop.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:In your code, next() returns a string. The first call to this is ok, but a subsequent call will return an empty string. You should try nextLine().
Are you sure? I think it will be the other way round. A next() call will return the next token assuming the default delimiter is used, but nextLine() may return the empty String "".
 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Carey Brown wrote:In your code, next() returns a string. The first call to this is ok, but a subsequent call will return an empty string. You should try nextLine().
Are you sure? I think it will be the other way round. A next() call will return the next token assuming the default delimiter is used, but nextLine() may return the empty String "".

Well it depends on the behavior that you expect. nextLine() will return an empty string if you just hit enter (this would be my preference because you can re-prompt). next() will block until some string is entered followed by enter, pressing enter alone does nothing and doesn't give the program a chance to re-prompt.

edit: I see my reasoning on my previous post didn't match what I was thinking.
 
Bruno Van Hemel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
It think you mean


edit: This while loop should be if they don't enter 'c' or 'f', in which case you should re-prompt. But you have get temperature inside this while loop.


Many thanks to everybody but the above quote was for met the solution.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you don't just take the code that works and move on. It would be to your benefit to understand why this works and why what you wrote originally didn't.
 
Bruno Van Hemel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I hope you don't just take the code that works and move on. It would be to your benefit to understand why this works and why what you wrote originally didn't.

yes you have right I do understand now why my code wouldn't work.
I'am also follow a course in  the evening-school.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!