从Java文件将数据存储到单独的数组 [英] java storing data from file into separate array

查看:134
本文介绍了从Java文件将数据存储到单独的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个文件到单独的数组存储数据的问题。以我为例,我需要阅读的文件和数据存储到不同的阵列,为飓风,风速,在pressure,并在今年的名称。下面是我的code,我到目前为止有:

I have a question on how to store data from a file into separate arrays. In my case, I need to read a file and store the data into separate arrays, one for the name of the hurricane, the wind speed, the pressure, and the year. Below is my code that I have so far:

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

任何帮助将大大AP preciated。

Any help will be greatly appreciated.

推荐答案

我想说

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中的指数相互对应,并可以很容易地在一个迭代通过循环。

Now the indices for each ArrayList correspond to one another and can easily be iterated through in a for loop.

这篇关于从Java文件将数据存储到单独的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆