[英]Reading in a .txt file and storing the values into a double array in Java
我在嘗試找出一種方法來嘗試讀取文本文件並將文件的值存儲到雙精度數組時遇到麻煩。 我正在使用Scanner
類,並且試圖在for循環中讀取文件,以便將所有值存儲到數組中。 但是.nextDouble()
與數組不兼容。 有沒有辦法將它們存儲在數組中? 我需要使用以不同方法讀取的文件中的單獨數字。
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[i] = inFile.nextDouble();
就目前而言,您的代碼將無法編譯,並且您實際上從未對從文件中提取的值進行任何操作。
z= inFile.nextDouble;
正在嘗試獲取一個double並將其分配給一個數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.