Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: BufferedReader error?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    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
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: BufferedReader error?

    Try this

    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");
            }
     
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default 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:

    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

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: BufferedReader error?

    Quote Originally Posted by copeg View Post
    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:

    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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: BufferedReader error?

    Quote Originally Posted by JavaPF View Post
    Good work copeg.
    Right back at ya! Your response was better than mine

Similar Threads

  1. Problem in BufferedReader
    By shamed in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 1st, 2009, 03:30 PM
  2. Scanner vs BufferedReader?
    By Bill_H in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 27th, 2009, 09:44 AM
  3. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM
  4. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 06:55 PM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM