Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Big Dave

macrumors 6502
Original poster
Nov 27, 2007
313
23
Crestview, Fl
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

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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
What does read return? Not a double. Maybe you could print your area, then area over pi, then the whole equation?

-Lee
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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.

Code:
import java.io.*;
import java.text.*;
[color=blue]// import java.lang.Math;  This is not needed[/color]

class Radius
   {
      public static void main (String args[])
         {
[color=blue]
            FileWriter fout;
[/color]
            try
                 {
[color=blue]
                    fout = new FileWriter ("radius.csv");
[/color]
                    double area;
                    System.out.println("Enter the area value");
[color=red]
                    BufferedReader inRdr = new BufferedReader(new InputStreamReader(System.in));
                    String input = inRdr.readLine();
                    area = Double.valueOf(input);
[/color]
                    double radius = (Math.sqrt( area / Math.PI));
                    System.out.println("The radius is: ");
                    System.out.println( radius );
[color=blue]
                    PrintWriter foutWtr = new PrintWriter(fout, true); 
                    foutWtr.print("The radius is: ");
                    foutWtr.println( radius);
[/color]
                 }

        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

macrumors 6502
Aug 21, 2009
439
0
Columbus, Ohio
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

macrumors 6502
Original poster
Nov 27, 2007
313
23
Crestview, Fl
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

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
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

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors 6502
Original poster
Nov 27, 2007
313
23
Crestview, Fl
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.