CircAnalyzer
CircAnalyzer

Reputation: 704

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:

% 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?

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.

Upvotes: 1

Views: 103

Answers (3)

user2238635
user2238635

Reputation: 56

I am not familiar with the Scanner API, but you can get the result in this way:

// 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());
    }

Upvotes: 0

rby
rby

Reputation: 786

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.

Upvotes: 0

rgettman
rgettman

Reputation: 178263

There are 2 lines before the content that you wish to read. Skip 2 lines.

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.

Using a proper regular expression for the delimiter helps:

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

Upvotes: 2

Related Questions