简体   繁体   中英

Reading in a .txt file and storing the values into a double array in Java

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

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM