如何在Java中将文本文件中的数据读入数组,跳过某些元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18748756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:43:43  来源:igfitidea点击:

How to read data from a text file into arrays in Java, skip certain elements

javaarrays

提问by user2769894

I need help with an assignment I have to do for my class. I have only just started with Java so i'm pretty new with it. I need to write a code that will scan numbers from a text file then calculate them and create some averages. I used a for loop to scan the file but I don't know how to scan it so it skips every third line. I need each column of text rather than each line. The text file contains 3 columns and 7 rows.

我需要帮助完成我必须为我的班级做的作业。我刚刚开始使用 Java,所以我对它很陌生。我需要编写一个代码来扫描文本文件中的数字,然后计算它们并创建一些平均值。我使用 for 循环来扫描文件,但我不知道如何扫描它,因此它每三行跳过一次。我需要每一列文本而不是每一行。文本文件包含 3 列和 7 行。

public static void main(String [] args){
    readData("Sample.in");
}

static void readData(String fileName){
    try{
        int[] breakfast= new int[7];
        int[] lunch=new int[7];
        int[] dinner= new int[7];

        File input = new File(fileName);        // Creates Scanner to read line from the text file
        Scanner scanLine = new Scanner(input);  // Reads entire line from the file

        String line= scanLine.nextLine();       // To read numbers from the Line buffer

        Scanner scanNumber = new Scanner(line); // to read three numbers from the line buffer

        for(int meal =0; meal<3; meal++){       // Checks whether the number is present in the line
            if(scanNumber.hasNextInt()){        // read number from the line buffer and store it in the calories variable

                int calories = scanNumber.nextInt();
                System.out.print(calories+" ");

            }
        }
        System.out.println("");
    } catch(FileNotFoundException e){                   // catches exception in case the file is missing
        System.out.println(e);
    }
}

Example input file:

示例输入文件:

200 1000 800 
450 845 1200
800 250 400 
0 1500 1800
600 500 1000
700 1400 1700
675 400 900

回答by DwB

Here is a non-code solution

这是一个非代码解决方案

  1. Open the file.
  2. Create the scanner.
  3. while (scanner.hasNextLine())
  4. use the scanner and read one line.
  5. split the line using String.split(); // read the api and use the version of split that matches your requirements.
  6. split returns an array. you care about elements 0, 1, and 2.
  7. Convert elements 0, 1, and 2 to an integer (read Integer in the api documentation).
  8. add elements 0, 1, and 2 to Lists.
  1. 打开文件。
  2. 创建扫描仪。
  3. 而 (scanner.hasNextLine())
  4. 使用扫描仪读取一行。
  5. 使用 String.split() 分割线;// 读取 api 并使用符合您要求的 split 版本。
  6. split 返回一个数组。你关心元素 0、1 和 2。
  7. 将元素 0、1 和 2 转换为整数(在 api 文档中阅读 Integer)。
  8. 将元素 0、1 和 2 添加到列表中。

Some java into:

一些java变成:

List<Integer> breakfast = new ArrayList<Integer>();
List<Integer> dinner = new ArrayList<Integer>();
List<Integer> lunch = new ArrayList<Integer>();

// add an element to breakfast:
breakfast.add(blammy);

// Iterate through dinner:
int dinnerTotal = 0
for (Integer current : dinner)
{
    dinnerTotal += current.intValue();
}