Re: BufferedReader error?
I have compiled your code but I do not get this error when I enter anything other than 1. I can clearly see it doesn't work though.
I don't really think the way you have coded this is the best method..
I will post an update shortly.
Re: BufferedReader error?
Try this :D
Code :
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ingrese la medida de temperature que quiere convertir(1: Celsius, 2: Farenheit, 3:Kelvin): ");
String myString = bf.readLine();
if(myString.equals("1"))
{
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Ingrese la temperatura Celsius que quiere convertir: ");
double c=Double.parseDouble(bf.readLine());
double kelvinC = c + 273.15;
double farenheitC = 1.8*c + 32;
System.out.print("La temperatura "+c+"°C es equivalente a "+farenheitC+"°F y "+kelvinC+"°K.");
}
else if(myString.equals("2"))
{
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ingrese la temperatura Farenheit que quiere convertir: ");
double f=Double.parseDouble(bf.readLine());
double celsiusF=(f-32)/1.8;
double kelvinF=(f+459.67)/1.8;
System.out.print("La temperatura "+f+"°F es equivalente a "+celsiusF+"°C y "+kelvinF+"°K.");
}
else if(myString.equals("3"))
{
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ingrese la temperatura Kelvin que quiere convertir: ");
double k=Double.parseDouble(bf.readLine());
double celsiusK=k-273.15;
double farenheitK=(9/5)*k - 459.67;
System.out.print("La temperatura "+k+"°K es equivalente a "+celsiusK+"°C y "+farenheitK+"°F");
}
}
}
Re: BufferedReader error?
If you press enter and read an empty line you will receive a NumberFormatException. Your program won't respond for 2 or 3 because you read the line upon checking for 1, if it is not one the program waits to read the next line. You should read the line first, then check the value:
Code :
String line = br.readLine();
if(1==Integer.parseInt(line))
Ideally, you would also have a way to validate the user entries rather than having your program exit with an exception.
EDIT: Doh! JavaPF beat me to the punch with a much more thorough response
Re: BufferedReader error?
Quote:
Originally Posted by
copeg
If you press enter and read an empty line you will receive a NumberFormatException. Your program won't respond for 2 or 3 because you read the line upon checking for 1, if it is not one the program waits to read the next line. You should read the line first, then check the value:
Code :
String line = br.readLine();
if(1==Integer.parseInt(line))
Ideally, you would also have a way to validate the user entries rather than having your program exit with an exception.
EDIT: Doh! JavaPF beat me to the punch with a much more thorough response
=:) Good work copeg.
Re: BufferedReader error?
Quote:
Originally Posted by
JavaPF
=:) Good work copeg.
Right back at ya! Your response was better than mine :o