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...