如何文件分割成多个阵列,显示它们,并在某些阵列进行计算 [英] how to split a file into multiple arrays, display them, and perform calculations on some arrays

查看:270
本文介绍了如何文件分割成多个阵列,显示它们,并在某些阵列进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何将文件的数据分割成多个阵列和某些阵列进行计算的问题。以我为例,我需要阅读飓风文件并打印出了名,年份,种类,pressure,并在其自己的数组风速每然后求其平均值的类别,pressure,以及风速,然后找出一个特定类别的数量,像第1类:26,我不知道如何做到这一点。我用in.nextInt(),但我相信它会在文件的行打印出所有的数字,然后去到下等,因为当我跑我的程序,我得到了一个错误。我怎么能写这样的程序?任何帮助将大大AP preciated。下面是我尝试读取该文件。

hurcdata2.txt:

  1980年八月945 100艾伦2
1983年962月100艾丽西亚2
1984年949月100戴安娜2
1985年1002年7月65鲍勃·1
1985年八月80 987 1丹尼
1985年959月100埃琳娜2
1985年九月90 942 1格洛丽亚
1985年十月75 971 1娟
1985年967月85凯特·1
1986年骏990 75邦尼1
1986年八月65 990 1查理
1987年十月65 993 1弗洛伊德
1988年九月70 984 1佛罗伦萨
1989年八月70 986 1尚塔尔
1989年九月934 120雨果3
1989年十月75 983 1杰里
1991年八月90 962 1鲍勃
1992年八月922 145安德鲁​​·4
1993年960月100艾米莉2
1995年八月85 973 1艾琳
1995年942月100蛋白石2
1996年七月90 974 1伯莎
1996年九月954 100弗兰2
1997年七月70 984 1丹尼
1998年八月95 964 1邦妮
1998年九月70 987 1伯爵
1998年九月90 964 1乔治
1999年八月951 100布雷特2
1999年九月90 956 1弗洛伊德
1999年十月70 987 1艾琳
2002年十月80 963 1丽丽
2003月80 979 1克洛
2003九月90 957伊莎贝尔1
2004年八月70 972 1亚历克斯
2004年八月941 130 4查理
2004年八月65 985 1加斯顿
2004年九月90 960 1弗朗西丝
2004年九月946 105伊万2
2004年九月950 105 2珍妮
2005年7月65 992 1辛迪
2005年7月930 130 4丹尼斯
2005年7月929 135艾米丽4
2005年8月85 975 1艾琳
2005年8月902 150 4卡特里娜
2005年9月960 100 2玛丽亚
2005年9月80 979 1内特
2005年9月80 976 1奥菲利亚
2005年9月70 985 1菲利普
2005年9月897 150丽塔4
2005年9月70 979 1斯坦
2005年9月65 987 1文斯
2005年9月882 150 4威尔玛
2005年9月960 100测试版2
2005年9月75 979 1小量
2006年8月65 995 1埃内斯托
2006年9月80 972 1佛罗伦萨
2006年9月955 105戈登2
2006年9月954 110 2海琳
2006年9月75 985 1以撒

下面是code为我的计划,我写了不能正常运行。

Hurricanes2.java:

 进口的java.io.File;
进口java.util.Scanner中;
进口java.io.IOException异常;
进口的java.util.ArrayList;公共类Hurricanes2 {公共静态无效的主要(字符串ARGS [])抛出IOException    //创建扫描仪INFILE
    文件fil​​ename =新的文件(/用户/ timothylee / hurcdata2.txt);    INT I = 0;    INT [] =值INT新[I]
    的String [] = HurrNames新的String [I]    扫描仪INFILE =新的扫描仪(文件名);    而(inFile.hasNext()){
        值[I] = inFile.nextInt();
        HurrNames [I] = inFile.next();
        我++;
        的System.out.println(值[I]);
        的System.out.println(HurrNames [I]);
    }
    inFile.close();
}
}

我得到这个当我运行它:

 运行:
异常线程mainjava.lang.ArrayIndexOutOfBoundsException:0
在Hurricanes2.main(Hurricanes2.java:22)
Java结果:1
BUILD SUCCESSFUL(总时间:1秒)


解决方案

这是你要去工作。

 扫描程序扫描=新的扫描仪(新的FileReader(新文件(hurcdata2.txt)));
    清单<整数GT;年=新的ArrayList<整数GT;();
    清单<串GT;月=新的ArrayList<串GT;();
    清单<整数GT;风速=新的ArrayList<整数GT;();
    清单<整数GT; pressure =新的ArrayList<整数GT;();
    清单<串GT; NAME =新的ArrayList<串GT;();
    清单<整数GT;类别=新的ArrayList<整数GT;();    而(scan.hasNext()){
        字符串输入= scan.nextLine();
        的String [] =飓风input.split(\\\\ S +);
        year.add(的Integer.parseInt(飓风[0]));
        month.add(飓风[1] .trim());
        windspeed.add(的Integer.parseInt(飓风[2]));
        pressure.add(的Integer.parseInt(飓风[3]));
        name.add(飓风[4] .trim());
        category.add(的Integer.parseInt(飓风[5]));
    }    INT总和= 0;
    对于(整数整数:pressure){
        总和+ = integer.intValue();
    }    双平均= SUM / pressure.size();
    //System.out.println(average);
}

I have a question on how to split a file's data into multiple arrays and perform calculations on certain arrays. In my case, I need to read a hurricanes file and print out the name, year, category, pressure and windspeed each in its own array and then find the average of the category, pressure, and windspeed and then find out the number of a specific category, like Category 1: 26. I don't know how to do this. I used in.nextInt() but I assume it would print out all the numbers in the line of the file and then go on to the next and so on because when I ran my program, I got an error. How can I write a program like this? Any help will be greatly appreciated. Below is the file that I am trying to read from.

hurcdata2.txt:

1980 Aug    945 100 Allen           2
1983 Aug    962 100 Alicia          2
1984 Sep    949 100 Diana           2
1985 Jul    1002    65  Bob         1
1985 Aug    987 80  Danny           1
1985 Sep    959 100 Elena           2
1985 Sep    942 90  Gloria          1
1985 Oct    971 75  Juan            1
1985 Nov    967 85  Kate            1
1986 Jun    990 75  Bonnie          1
1986 Aug    990 65  Charley         1
1987 Oct    993 65  Floyd           1
1988 Sep    984 70  Florence        1
1989 Aug    986 70  Chantal         1
1989 Sep    934 120 Hugo            3
1989 Oct    983 75  Jerry           1
1991 Aug    962 90  Bob         1
1992 Aug    922 145 Andrew          4
1993 Aug    960 100 Emily           2
1995 Aug    973 85  Erin            1
1995 Oct    942 100 Opal            2
1996 Jul    974 90  Bertha          1
1996 Sep    954 100 Fran            2
1997 Jul    984 70  Danny           1
1998 Aug    964 95  Bonnie          1
1998 Sep    987 70  Earl            1
1998 Sep    964 90  Georges         1
1999 Aug    951 100 Bret            2
1999 Sep    956 90  Floyd           1
1999 Oct    987 70  Irene           1
2002 Oct    963 80  Lili            1
2003 Jul    979 80  Claudette       1
2003 Sep    957 90  Isabel          1
2004 Aug    972 70  Alex            1
2004 Aug    941 130 Charley         4
2004 Aug    985 65  Gaston          1
2004 Sep    960 90  Frances         1
2004 Sep    946 105 Ivan            2
2004 Sep    950 105 Jeanne          2
2005 Jul    992 65  Cindy           1
2005 Jul    930 130 Dennis          4
2005 Jul    929 135 Emily           4
2005 Aug    975 85  Irene           1
2005 Aug    902 150 Katrina         4
2005 Sep    960 100 Maria           2
2005 Sep    979 80  Nate            1
2005 Sep    976 80  Ophelia         1
2005 Sep    985 70  Phillipe        1
2005 Sep    897 150 Rita            4
2005 Sep    979 70  Stan            1
2005 Sep    987 65  Vince           1
2005 Sep    882 150 Wilma           4
2005 Sep    960 100 Beta            2
2005 Sep    979 75  Epsilon         1
2006 Aug    995 65  Ernesto         1
2006 Sep    972 80  Florence        1
2006 Sep    955 105 Gordon          2
2006 Sep    954 110 Helene          2
2006 Sep    985 75  Isaac           1

Below is the code for my program that I wrote that does not run correctly

Hurricanes2.java:

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

public class Hurricanes2{

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

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

    int i = 0;

    int[] values = new int[i];
    String[] HurrNames = new String[i];

    Scanner inFile = new Scanner(filename);

    while(inFile.hasNext()){
        values[i] = inFile.nextInt();
        HurrNames[i] = inFile.next();
        i++;
        System.out.println(values[i]);
        System.out.println(HurrNames[i]);
    }
    inFile.close();
}
}

I get this when I run it:

run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Hurricanes2.main(Hurricanes2.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

解决方案

This is gonna work for you.

    Scanner scan = new Scanner(new FileReader(new File("hurcdata2.txt")));
    List<Integer> year = new ArrayList<Integer>();
    List<String> month = new ArrayList<String>();
    List<Integer> windspeed = new ArrayList<Integer>();
    List<Integer> pressure = new ArrayList<Integer>();
    List<String> name = new ArrayList<String>();
    List<Integer> category = new ArrayList<Integer>();

    while(scan.hasNext()){
        String input = scan.nextLine();
        String[] hurricane = input.split("\\s+");
        year.add(Integer.parseInt(hurricane[0]));
        month.add(hurricane[1].trim());
        windspeed.add(Integer.parseInt(hurricane[2]));
        pressure.add(Integer.parseInt(hurricane[3]));
        name.add(hurricane[4].trim());
        category.add(Integer.parseInt(hurricane[5]));
    }

    int sum = 0;
    for(Integer integer : pressure){
        sum += integer.intValue();
    }

    double average = sum / pressure.size();
    //System.out.println(average);
}

这篇关于如何文件分割成多个阵列,显示它们,并在某些阵列进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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