Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 14, 2011, 08:54 PM   #1
Big Dave
macrumors 6502
 
Join Date: Nov 2007
Location: Crestview, Fl
Java - correct my calculation?

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);
           }
         }
   }
Big Dave is offline   0 Reply With Quote
Old Apr 14, 2011, 09:08 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
What does read return? Not a double. Maybe you could print your area, then area over pi, then the whole equation?

-Lee
lee1210 is offline   0 Reply With Quote
Old Apr 14, 2011, 09:28 PM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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.*;
// 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.
jiminaus is offline   0 Reply With Quote
Old Apr 14, 2011, 09:33 PM   #4
Bostonaholic
macrumors 6502
 
Join Date: Aug 2009
Location: Columbus, Ohio
System.in.read() returns an 'int'

Quote:
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.
__________________
17" MBP 5,2 | 13" MBA 6,2 | iPhone 5S | iPad 2

Bostonaholic is offline   0 Reply With Quote
Old Apr 14, 2011, 09:59 PM   #5
Big Dave
Thread Starter
macrumors 6502
 
Join Date: Nov 2007
Location: 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
Big Dave is offline   0 Reply With Quote
Old Apr 15, 2011, 12:10 AM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Big Dave View Post
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.
chown33 is offline   0 Reply With Quote
Old Apr 15, 2011, 05:32 AM   #7
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Big Dave View Post
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.
gnasher729 is offline   0 Reply With Quote
Old Apr 15, 2011, 03:01 PM   #8
Big Dave
Thread Starter
macrumors 6502
 
Join Date: Nov 2007
Location: Crestview, Fl
Quote:
Originally Posted by gnasher729 View Post
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.
Big Dave is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Time Calculation as shift in iOS arungre iPhone/iPad Programming 6 Nov 29, 2013 03:13 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
MacRumors update links Safari & Java! Please correct them!!! BDM STUDIOS NL OS X 10.8 Mountain Lion 0 Apr 16, 2013 07:24 PM

Forum Jump

All times are GMT -5. The time now is 08:42 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC