PDA

View Full Version : Java - correct my calculation?




Big Dave
Apr 14, 2011, 08:54 PM
I wrote a simple Java program that is supposed to calculate the radius of a circle if the area is given. My program is returning the wrong value for radius and I was wondering if anyone could show me my error.
Thanks,

Big Dave


import java.io.*;
import java.text.*;
import java.lang.Math;

class Radius
{
public static void main (String args[])
{
FileOutputStream fout;
try
{
fout = new FileOutputStream ("radius.csv");
double area;
System.out.println("Enter the area value");
area = System.in.read();
double radius = (Math.sqrt( area / Math.PI));
System.out.println("The radius is: ");
System.out.println( radius );
new PrintStream(fout).print("The radius is: ");
new PrintStream(fout).println( radius);
}

catch (IOException e)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
}



lee1210
Apr 14, 2011, 09:08 PM
What does read return? Not a double. Maybe you could print your area, then area over pi, then the whole equation?

-Lee

jiminaus
Apr 14, 2011, 09:28 PM
System.in is only an InputStream. So System.in.read() only reads in the next byte. What you're actually reading is the ASCII value of the first digit.

You probably want to wrap in an InputStreamReader and then in a BufferedReader, so you call readLine(). This will get the input into a String.

Then use Double.valueOf(String) to parse the string into a double.


import java.io.*;
import java.text.*;
// import java.lang.Math; This is not needed

class Radius
{
public static void main (String args[])
{

FileWriter fout;

try
{

fout = new FileWriter ("radius.csv");

double area;
System.out.println("Enter the area value");

BufferedReader inRdr = new BufferedReader(new InputStreamReader(System.in));
String input = inRdr.readLine();
area = Double.valueOf(input);

double radius = (Math.sqrt( area / Math.PI));
System.out.println("The radius is: ");
System.out.println( radius );

PrintWriter foutWtr = new PrintWriter(fout, true);
foutWtr.print("The radius is: ");
foutWtr.println( radius);

}

catch (IOException e)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
}


BTW Don't use output streams when writing text files; use writers. Same for input, use readers not input streams.

Bostonaholic
Apr 14, 2011, 09:33 PM
System.in.read() returns an 'int'

public abstract int read() throws IOException

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Big Dave
Apr 14, 2011, 09:59 PM
Thanks guys. This helps. My Java book doesn't discuss the "cin" type operations that I used in C. I suppose entering data from the command line is old school.

-Big Dave

chown33
Apr 15, 2011, 12:10 AM
My Java book doesn't discuss the "cin" type operations that I used in C.

Which cin operations are in C? I thought cin was a C++ thing.


In Java, see the class java.util.Scanner. In particular, see nextDouble().

If your book hasn't covered Scanner, you might have to try one of the suggestions already posted.

gnasher729
Apr 15, 2011, 05:32 AM
I wrote a simple Java program that is supposed to calculate the radius of a circle if the area is given. My program is returning the wrong value for radius and I was wondering if anyone could show me my error.

What about some basic techniques to find the error yourself: You know the program doesn't do what you want it to do. You don't know what is wrong. You don't actually know what the program does. It would be much easier to find what's wrong if you first find out what the program does. The first step there would have been to print out the area.

Had you done that, you would have seen immediately that your assumption about the problem was wrong from the start. The problem was not with the calculation at all, it was with reading the area.

Big Dave
Apr 15, 2011, 03:01 PM
What about some basic techniques to find the error yourself: You know the program doesn't do what you want it to do. You don't know what is wrong. You don't actually know what the program does. It would be much easier to find what's wrong if you first find out what the program does. The first step there would have been to print out the area.

Had you done that, you would have seen immediately that your assumption about the problem was wrong from the start. The problem was not with the calculation at all, it was with reading the area.

You're right. I should have done the error checking.