简体   繁体   English

JAVA读取数值数据并存储到arraylist

[英]JAVA Read numeric data and store to arraylist

I have a text file with the following content that I am trying to read into an ArrayList: 我有一个文本文件,该文件包含以下内容,我正在尝试将其读取到ArrayList中:

% There are 600 students. Each row is the set of desired courses (numbered from 1 to 18) for that student. 

9.     13.    3.     7.     5.     8.     6.     12.    14.    11.    18.    15.  
12.    1.     4.     16.    8.     5.     14.    3.     2.     6.     18.    9.   
4.     16.    9.     13.    17.    6.     8.     14.    3.     11.    15.    10.  
4.     16.    9.     13.    14.    11.    2.     15.    5.     10.    17.    8.   
4.     16.    12.    1.     18.    14.    9.     17.    8.     5.     6.     11.  
4.     16.    12.    1.     8.     5.     14.    11.    18.    10.    15.    2.   
9.     13.    12.    1.     11.    10.    18.    8.     4.     2.     5.     15.  
9.     13.    3.     7.     14.    11.    10.    4.     15.    18.    12.    17.  
12.    1.     3.     7.     5.     10.    11.    6.     18.    14.    8.     9.   
9.     13.    12.    1.     18.    10.    17.    3.     6.     14.    8.     15.

Below is my code, however it is not reading anything: 下面是我的代码,但是它什么也没读:

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SchedReader {

    public static void main(String[] args){

        //reading   
        try{

            File homedir = new File(System.getProperty("user.home"));
            File fileToRead = new File(homedir, "workspace/Project1/sched.txt");

            List<Integer> integers = new ArrayList<Integer>();

            Scanner fileScanner = new Scanner(fileToRead);

            fileScanner.nextLine();

            fileScanner.useDelimiter(". ");

            while (fileScanner.hasNextInt())
            {
               integers.add(fileScanner.nextInt());
            }

            System.out.println("Arraylist contains: " + integers.toString());
        }       
        catch (Exception e){
            System.out.println(e.toString());
        }


    }
}

Here is my output: 这是我的输出:

Arraylist contains: []

Could anybody please suggest how I can change my code to be able to load all the data into the ArrayList? 有人可以建议我如何更改代码以将所有数据加载到ArrayList中吗?

EDIT 编辑

So here's how I changed the code to advance through each line: 因此,这是我更改代码以在每一行中前进的方式:

    fileScanner.useDelimiter("[\\. \\n]+");

    while (fileScanner.hasNextInt())
    {
       integers.add(fileScanner.nextInt());
       fileScanner.hasNextLine();
       fileScanner.nextLine();

    }

However, it is still outputting to just one line. 但是,它仍然只输出到一行。

There are 2 lines before the content that you wish to read. 您要阅读的内容前有2行。 Skip 2 lines. 跳过2行。

fileScanner.nextLine();
fileScanner.nextLine();

You're attempting to use the space ' ' and dot '.' 您正在尝试使用空格' '和点'.' characters as delimiters, but it's interpreted as a regular expression, meaning the separator must be any character followed by exactly one space. 字符作为分隔符,但它被解释为正则表达式,这意味着分隔符必须是任何字符,后跟一个空格。 As you have multiple spaces in between numbers, there is no next int token. 由于数字之间有多个空格,因此没有下一个int标记。

Using a proper regular expression for the delimiter helps: 为分隔符使用正确的正则表达式有助于:

fileScanner.useDelimiter("[\\. ]+");  // One or more of . and space

You get a single line with the current code because it is not advancing to subsequent lines. 您会在当前代码中得到一行,因为它没有前进到后续行。 Add fileScanner.hasNextLine() with fileScanner.nextLine() to advance to the next line and you can use the corrected delimiter to read in the ints. 将fileScanner.hasNextLine()与fileScanner.nextLine()一起添加到下一行,您可以使用更正的分隔符读取int。

I am not familiar with the Scanner API, but you can get the result in this way: 我不熟悉Scanner API,但是您可以通过以下方式获得结果:

// reading
    try {
        File fileToRead = new File(SchedReader.class.getResource("afile").getPath());

        List<Integer> integers = new ArrayList<Integer>();

        BufferedReader br = new BufferedReader(new FileReader(fileToRead));
        br.readLine();
        br.readLine();
        String line = null;
        while((line = br.readLine())!= null){
            String[] values = line.split("\\.");
            for(String value: values){
                if(value.trim().length() > 0){
                    integers.add(Integer.parseInt(value.trim()));
                }
            }
        }

        br.close();

        System.out.println("Arraylist contains: " + integers.toString());
    } catch (Exception e) {
        System.out.println(e.toString());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM