• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prob with BufferedReader(System.in)

 
George Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


After the Formula is done and I type d I get a weird error that Stops the prgm. HELP ME FIX IT
 
mathew john
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the Error You are getting .if You are getting a Number Format Exception .That is because the application has attempted to convert a character or a string to one of the numeric types, but that the string does not have the appropriate format.So you cannot enter a String or Character .

if You are getting this Error Value out of range You better check out the ParseByte .You can check the ParseFloat and ParseDouble to accomadate higher range .
 
George Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error I get is Exception in thread "main" java.langNumberFormatException: empty String

at java.lang.FloatingDecimal.readJavaFormatString<FloatingDecimal.java:983>
at java.lang.Float.parseFloat<float.java:222>
at Fahrenheit.main<Fahrenheit.java:14>
 
Norm Radder
Ranch Hand
Posts: 1014
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Break the following line into separate statements and test/display results.
Ie read the data, test data present, echo the input(for testing) and then convert it.

Or use: try{}catch() block to catch Exceptions

In your code: Which line is line 14? The one with the error?
 
George Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea
 
James Hejmanowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From one greenhorn to another...

Sorry about the fonts... I don't know how to quote code in the body of the message yet... In any event the system hangs on reaching lines 17 and 18 in your original code below (commented below); these lines appear to be un-necessary. Commenting these lines out yields runnable code; though input values are limited to a range of -128 to 127 (Byte value range).

One thought... Why re-declare br with every iteration of your loop? the code runs with "BufferedReader br = new BufferedReader(new InputStreamReader(System.in));" placed before the loop.


code:

import java.io.*;
import java.lang.Character;
import java.applet.Applet;
import java.lang.String;
class Fahrenheit {
public static void main(String args[]) throws Exception {
float realFahrenheit;
float Celsius;
char Answer;
int Yay;
do{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter F: ");
realFahrenheit = Byte.parseByte(br.readLine().trim());
Celsius = (realFahrenheit - 32) * 5 / 9;
System.out.println(realFahrenheit+" F is "+Celsius+" C");
Answer = (char)System.in.read(); //Appears un-needed.
System.in.read(); //Appears un-needed.
Answer = Character.toUpperCase(Answer);
}while(Answer != 'Q');
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic