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
}
z= inFile.nextDouble;That line doesn't even compile. And the result probably should be stored inz[i].