• August 14th, 2011, 12:31 PM
Akarsh
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
{
System.out.println("A.Volume of cube\nB.Volume of sphere\nC.Volume of cuboid\nEnter choice according to the menu.");
final double P=3.14;
Volume ob1=new Volume();
if(ch=='A')
{
System.out.println("Enter the side of the cube...");
ob1.volu(s);
}
if(ch=='B')
{
System.out.println("Enter the radius of the sphere...");
ob1.volu(r,P);
}
if(ch=='C')
{
System.out.println("Enter the length, breadth and height of the cuboid...");
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);
}
{
System.out.println("Volume of sphere:");
System.out.println(vol);
}
public void volu(double length,double breadth,double height)
{
System.out.println("Volume of cuboid:");
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 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')
{
System.out.println("Enter the side of the cube...");
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...
• August 15th, 2011, 04:27 AM
piulitza
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
Quote:

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

Scanner in = new Scanner(new File(url));
and there url is the path to the file.
• August 15th, 2011, 05:00 PM
Sean4u
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'...

Code java:

```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) + ")"); } } }```
• August 15th, 2011, 05:37 PM
Norm