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 4 of 4

Thread: 'BufferedReader' problem...

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 'BufferedReader' problem...

    hi every1...
    i am new on this site...
    i need help...
    here is the program...

    /*Program with class name 'Volume' using function overloading to compute the volume of cube, a sphere and a cuboid.Let the user input the values and his choice*/
    import java.io.*;
    class Volume
    {
    public void main() throws IOException
    {
    BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("A.Volume of cube\nB.Volume of sphere\nC.Volume of cuboid\nEnter choice according to the menu.");
    char ch=(char)ob.read();
    final double P=3.14;
    Volume ob1=new Volume();
    if(ch=='A')
    {
    System.out.println("Enter the side of the cube...");
    double s=Double.parseDouble(ob.readLine());
    ob1.volu(s);
    }
    if(ch=='B')
    {
    System.out.println("Enter the radius of the sphere...");
    double r=Double.parseDouble(ob.readLine());
    ob1.volu(r,P);
    }
    if(ch=='C')
    {
    System.out.println("Enter the length, breadth and height of the cuboid...");
    double l=Double.parseDouble(ob.readLine());
    double b=Double.parseDouble(ob.readLine());
    double h=Double.parseDouble(ob.readLine());
    ob1.volu(l,b,h);
    }
    }
    public void volu(double side)
    {
    System.out.println("Volume of cube:");
    double vol=side*side*side;
    System.out.println(vol);
    }
    public void volu(double radius,double pi)
    {
    System.out.println("Volume of sphere:");
    double vol=(4/3)*pi*radius*radius*radius;
    System.out.println(vol);
    }
    public void volu(double length,double breadth,double height)
    {
    System.out.println("Volume of cuboid:");
    double vol=length*breadth*height;
    System.out.println(vol);
    }
    }



    Output

    A.Volume of cube
    B.Volume of sphere
    C.Volume of cuboid
    Enter choice according to the menu.
    A
    Enter the side of the cube...

    [after this the following error is observed]

    java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:994)
    at java.lang.Double.parseDouble(Double.java:510)
    at Volume.main(Volume.java:15)


    so, this is the program...

    i hv also found a solution to this...
    the solution is that if a make a new object inside every if block, then the number can be inputted and the error is not observed(for example)

    if(ch=='A')
    {
    BufferedReader ob2=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the side of the cube...");
    double s=Double.parseDouble(ob2.readLine());
    ob1.volu(s);
    }


    but, my question is that why cant we input a number using BufferedReader inside the if block, 'if' is not a function that we have to declare a variable again and again...

    so this is it...
    plz HELP...


  2. #2
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 'BufferedReader' problem...

    Looks like BufferedReader does not have any method to read doubles, i have eclipse and that one shows me all methods of this class. I am not that experienced and i think that the keyboard is the input for your program, and if you want to read from keyboard using this constructor
    Scanner tastatura = new Scanner(System.in);
    , Scanner class has method to read double from keyboard. And Scanner can also read from files with this one
    Scanner in = new Scanner(new File(url));
    and there url is the path to the file.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: 'BufferedReader' problem...

    I'd say your problem is caused by console input - you can't capture keystrokes from the command line because the command line is typically editable: you don't *input* characters until you press Enter. Consider the output of this program and what might be happening in your program if the 'A' is being read from an input buffer that contains more than just 'A'...

    package com.javaprogrammingforums.domyhomework;
     
    import java.io.*;
     
    public class BufferedReaderReadChar
    {
      public static void main(String[] args) throws Exception
      {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true)
        {
          System.out.print("Enter some characters: ");
          char c = (char)br.read();
          if (c >= 0x20) /* printable */
            System.out.println("You typed '" + c + "'");
          else /* special character */
            System.out.println("You typed (0x" + Integer.toString(c, 0x10) + ")");
        }
      }
    }
    Last edited by Sean4u; August 15th, 2011 at 05:02 PM. Reason: Bad markup

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 'BufferedReader' problem...

    NumberFormatException: empty String
    With just a bit more programming you can prevent this.
    Read the user's input into a String and test if the String is empty. If it is ask the user to enter again.
    If you want to force the user to enter good data, you could write a method to prompt the user for the data, test for good data and ask again if it is bad. Otherwise convert it to a double and return the double.
    Another thing to consider is to put the parseDouble call in try{}catch block to catch bad data.

    And another consideration: Use the Scanner class. It has methods to test if the next data is a double before you read it.

Similar Threads

  1. [SOLVED] Reading a file with bufferedreader
    By ppata in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 20th, 2010, 12:20 PM
  2. BufferedReader
    By reg4ltip in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2010, 12:15 PM
  3. BufferedReader error?
    By Umogrim in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 28th, 2010, 08:51 PM
  4. Problem in BufferedReader
    By shamed in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 1st, 2009, 04:30 PM
  5. 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