Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java将文件中的数据存储到单独的数组中_Java_Arrays_File Io - Fatal编程技术网

java将文件中的数据存储到单独的数组中

java将文件中的数据存储到单独的数组中,java,arrays,file-io,Java,Arrays,File Io,我有一个关于如何将文件中的数据存储到单独的数组中的问题。在我的例子中,我需要读取一个文件并将数据存储到单独的数组中,一个用于飓风名称、风速、压力和年份。以下是我迄今为止的代码: import java.util.Scanner; import java.io.File; import java.io.IOException; public class Hurricanes2 { public static void main(String args[]) throws IOException

我有一个关于如何将文件中的数据存储到单独的数组中的问题。在我的例子中,我需要读取一个文件并将数据存储到单独的数组中,一个用于飓风名称、风速、压力和年份。以下是我迄今为止的代码:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class Hurricanes2 {

public static void main(String args[]) throws IOException{

    // create token1
    String token1 = "";

    // create Scanner inFile
    Scanner inFile = new Scanner(new File("/Users/timothylee/hurcdata2.txt"));

    // while statment
    while(inFile.hasNext()){

        // initialize token1
        token1 = inFile.next();
    }

    // close inFile
    inFile.close();
}

}
我的文件包含以下内容:

1980 Aug    945 100 Allen
1983 Aug    962 100 Alicia
1984 Sep    949 100 Diana
1985 Jul    1002    65  Bob
1985 Aug    987 80  Danny
1985 Sep    959 100 Elena
1985 Sep    942 90  Gloria

非常感谢您的帮助。

您听说过正则表达式吗?使用令牌数组、两个字符串数组和正则表达式\s+可以解析输入

我想要点像这样的

    String tokens[];
    String input;
    String date[10] = new String[10];
    String hurricaneName[10] = new String[10];
    int count = 0;
    String temp;

      // while statment
input = inFile.hasNext()
while(input != null){
     tokens = input.split("\\s+");
     // the above line splits the input up at every white space
     // use this to put appropriate data into proper arrays for example
     temp = tokens[0] + tokens[1];
     date[count] = temp;

     hurricaneName[count] = tokens[5];         

    count ++;
    token1 = inFile.next();
}
我会说

String temp = "";
List yearList = new ArrayList();
List pressureList = new ArrayList();
List speedList = new ArrayList();
List nameList = new ArrayList();

while (temp = inFile.nextLine())
{
    String[] stemp = temp.split(" ");
    yearList.add(stemp[0]);
    //skipping the month
    pressureList.add(stemp[2]);
    speedList.add(stemp[3]);
    nameList.add(stemp[4]);
}

现在,每个ArrayList的索引彼此对应,可以很容易地在for循环中进行迭代。

只需基于空间分割测试,并将数据复制到各自的数组中即可