Results 1 to 3 of 3
  1. #1
    Umogrim is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default BufferedReader error?

    Hello all, I am new to this forum, and also to java, so bear with me a bit.
    I got this exercise in lab class to make a program that converts from one type of temperature to the other two(eg. Celsius to farenheit and kelvin). Now, I was progressing along nicely, and managed to make the first one(for Celsius) work, however Farenheit and Kelvin converters don't work. I asked my lab helper what was wrong, and he was stumped. The IDE(netbeans 6.8) isn't showing any underlined red errors, so I hope someone here could help me.

    Java 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): ");
    
            if(1==Integer.parseInt(bf.readLine()))
            {
                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.");            
            }
    
            if(2==Integer.parseInt(bf.readLine()))
            {
                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(3==Integer.parseInt(bf.readLine()))
            {
                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");
            }
            
    
        }
    
    }
    When it prompts me with "Ingrese la medida de temperature que quiere convertir(1: Celsius, 2: Farenheit, 3:Kelvin): ", any answer other than 1 gives me this error:

    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:499)
    at laboratorio1.Main.main(Main.java:32)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 14 seconds)

    I hope someone can help me, thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Umogrim View Post
    Java 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): ");
    
            if(1==Integer.parseInt(bf.readLine()))
            {
                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.");            
            }
    
            if(2==Integer.parseInt(bf.readLine()))
            {
                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(3==Integer.parseInt(bf.readLine()))
            {
                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");
            }
            
    
        }
    
    }
    A few corrections:

    1) Delete all of these lines in your if-statements; they're useless:

    Java Code:
                new BufferedReader(new InputStreamReader(System.in));
    2) After you have read a number (by reading a line) don't read any other lines, i.e. before your three if statements do this instead:

    Java Code:
            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 line= bf.readLine();
    int choice= Integer.parseInt(line);
    ... and change your ifs to this:

    Java Code:
    if (choice == 1) { ... }
    else if (choice == 2) { ... }
    else if (choice == 3) { ... }
    kind regards,

    Jos

  3. #3
    Umogrim is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks a bunch JosAH! That did the trick. You really saved me(and my lazy partner) on this one.

Similar Threads

  1. How to use BufferedReader?
    By ProgramBeginner in forum New To Java
    Replies: 4
    Last Post: 10-11-2011, 03:56 PM
  2. BufferedReader
    By Nerijus in forum New To Java
    Replies: 4
    Last Post: 04-26-2010, 08:28 PM
  3. BufferedReader, need help!
    By zacharyrod in forum New To Java
    Replies: 10
    Last Post: 11-19-2009, 11:56 AM
  4. BufferedReader error cannot resolve symbol
    By SwEeTAcTioN in forum New To Java
    Replies: 12
    Last Post: 10-22-2009, 06:22 AM
  5. BufferedReader
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 02:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •