0

I am having trouble trying to figure out a way trying to read in text file and storing the values of the file into a double array. I am using the Scanner class and I am trying to read it the file in a for loop so that all of the values are stored into the array. But the .nextDouble() is not compatible with the array. Is there a way that I can store them in an array? I need to use separate numbers from the file that is being read in different methods.

   public class Weight1
    {
        import java.util.Scanner;
        import java.io.File;
        import java.io.IOException;
         public static double  gravity (double[] z) throws IOException
            {
                File fileName= new File("gravity.txt");
                Scanner inFile= new Scanner(fileName);
                for( int i=0; i<z.length; i++)
                {
                z= inFile.nextDouble;
            }
                return z;
        }
 public static void main(String[] args)throws IOException
    {

        // Extension idea... instead of hard codeing the weight, you may propt the user for input.

        double earthWeight = 100.0; // initalize Earth weight to 100 lbs
        double [] z= new double [9];
        double gravity;

        String  token= "";
        String [] token1= new String [9];
        String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
                              // static method you write
         // static method you write
                  // static method you write

    }
1
  • 2
    z= inFile.nextDouble; That line doesn't even compile. And the result probably should be stored in z[i]. Commented Dec 28, 2013 at 19:57

1 Answer 1

1

Use

z[i] = inFile.nextDouble();

As it stands right now your code won't compile and you are never actually doing anything with the values that you pull from the file.

z= inFile.nextDouble; is trying to take a double and assign it to an array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.