Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java static method writing to a file

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java static method writing to a file

    Could someone lead me to the right direction of how I can write my result to a text file? I know how to write to a text file when they're all under one method but can't seem to figure out how when it static method. Thank you

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.File;
    public class GravityV1
    {
        // Calculate surface gravity
        public static double[] surfaceGrav( double []massKilo, double[] diameter)
        {   double gravConstant = 6.67E-11;
            double []surfaceGravi= new double[9];
            for(int count = 0; count < surfaceGravi.length;count++)
            {
            surfaceGravi[count] = ((gravConstant * massKilo[count]) / (Math.pow(diameter[count]/2,2)));
            }
            return surfaceGravi;
        }
     
        //Print the results
        public static void printResults(String [] planetName, double [] diameterKm, double [] massKilo, double[]gravity)
        {   
            System.out.printf("%32s\n","Planetary Data");
            System.out.printf("%-12s %-17s %7s %11s\n","Planet","Diameter(Km)","Mass(Kg)","g(m/s^2)");
            System.out.println("--------------------------------------------------");        
            for(int counter = 0; counter < 9; counter++)
            {
                System.out.printf("%-14s %-14.0f %5.2e %11.2f\n",planetName[counter],diameterKm[counter],massKilo[counter],gravity[counter]);
            }
        }
     
        //Output the results
        public static  void outputData(double[] gravity)throws IOException
        {
           PrintWriter outFile = new PrintWriter(new File("surfaceGravity.txt"));
            double []outputGrav = gravity; 
            for(int index = 0; index < outputGrav.length;index++)
            {
                outFile.println(outputGrav[index]);
            }
        }
     
        public static void main(String[]args) throws IOException
        {
            String [] planetName = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
            double [] massKilo = {3.30E23,4.869E24,5.972E24,6.4219E23,1.900E27,5.68E26,8.683E25,1.0247E26,1.27E22};
            double [] diameter = {4880000,12103600,12756300,6794000,142984000,120536000,51118000,49532000,2274000};
            double [] diameterKm = {4880,12103.6,12756.3,6794,142984,120536,51118,49532,2274 };
            double [] gravity = surfaceGrav(massKilo, diameter);
            printResults(planetName, diameterKm, massKilo, gravity);
            outputData(gravity);
     
     
        }
    }
     
     
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java static method writing to a file

    Please post the full text of any error messages you get.

    can't seem to figure out how when it static method.
    If there are no errors, please describe the problem you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java static method writing to a file

    Nevermind, you can delete the thread if needed Norm. I figured it out. Thank you.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java static method writing to a file

    Please mark as solved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Java Static Method
    By maple1100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 3rd, 2013, 09:09 PM
  2. Generating a Batch File - Static Method Error
    By Stroypet in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2012, 11:42 AM
  3. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  4. Writing output from a void method to a file
    By TheWhopper858 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 05:21 AM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM