1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Java - correct my calculation?

Discussion in 'Mac Programming' started by Big Dave, Apr 14, 2011.

  1. macrumors 6502

    #1
    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);
               }
             }
       }
    
     
  2. macrumors 68040

    lee1210

    #2
    What does read return? Not a double. Maybe you could print your area, then area over pi, then the whole equation?

    -Lee
     
  3. macrumors 65816

    jiminaus

    #3
    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.
     
  4. macrumors 6502

    Bostonaholic

    #4
    System.in.read() returns an 'int'

     
  5. macrumors 6502

    #5
    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
     
  6. macrumors 603

    #6
    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.
     
  7. macrumors G5

    gnasher729

    #7
    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.
     
  8. macrumors 6502

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

Share This Page