Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

My program has a bug. Need help

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im trying to get rid of bug. When i ask the user to enter miles or kilometers choice, It sort of stalls. I have to hit enter twice.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem in a nutshell: BufferedReader is buffered, but so is, usually, standard input. Therefore, if you use a BufferedReader on standard input, there are two different buffers of different sizes (one in Java, one in your OS's console driver), and filling up one won't necessarily fill up the other.
Anyway, you're using BufferedReader just to get the readLine() method; you can turn buffering off. Just use

That "1" says use a buffer size of one character -- i.e., no buffer. You should generally do this any time you use BufferedReader with standard input.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure that the program you've provided here won't even compile, let alone run. Are you sure this is the latest version?
A few thoughts. First, since this program is command-line only, you could just as easily extend "Object" (remove the "extends Frame" clause) and get rid of the awt imports.
Second, you define m, k, h, min and sec as "int" variables at the top of the program, then you use them (in Integer.parseInt(...) expressions) before they've been set. Then you try and shadow (re-define) them as "String" variables inside the if statements. It would probably be best to define them as "String" variables at the top, and do the "Integer.parseInt()" expressions AFTER reading the values in.
The if(miles_units)/else statements are mismatched. And you are trying to use a boolean value (miles_units) to handle three cases (miles, kilometers, invalid argument). A boolean value can handle only two cases.
Overall I think you have the necessary steps to do what you want, you just need to massage it a bit.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic