How to create array from text file using Scanner?

3.8k views Asked by At

I'm just starting to learn Java and I'm trying to complete this exercise.

I've understood how to extract the information from the txt file (I think) using scanner (we're only supposed to change the method body). However I'm not sure of the correct syntax to transfer the information to an array.

I realise it must be very simple, but I can't seem to figure it out. Could someone please point me in the right direction in terms of syntax and elements needed? Thank you in advance!

import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;

public class Lab02Task2 {

    /**
     * Loads the game records from a text file.
     * A GameRecord array is constructed to store all the game records.
     * The size of the GameRecord array should be the same as the number of non-empty records in the text file.
     * The GameRecord array contains no null/empty entries.
     * 
     * @param reader    The java.io.Reader object that points to the text file to be read.
     * @return  A GameRecord array containing all the game records read from the text file.
     */
    public GameRecord[] loadGameRecord(java.io.Reader reader) {

        // write your code after this line

        Scanner input = new Scanner(reader);
        for (int i=0; input.hasNextLine(); i++) {
            String inputRecord = input.nextLine();
            input = new Scanner(inputRecord);
            // array?
        }
        return null; // this line should be modified/removed after finishing the implementation of this method.
    }
}
4

There are 4 answers

0
Mordechai On

In case you already have a String of the file content, you can say:

String[] words = content.split("\\s");
0
Nicolas Buquet On

You can parse your String like this:

private ArrayList<String> parse(BufferedReader input) throws CsvException {
    ArrayList<String> data = new ArrayList<>();

    final String recordDelimiter = "\\r?\\n|\\r";
    final String fieldDelimiter = "\\t";

    Scanner scanner = new Scanner(input);
    scanner.useDelimiter(recordDelimiter);

     while( scanner.hasNext() ) {
        String line = scanner.next();
        data.add(line);
     }

     return data;
}

The input text will be scanned line by line.

2
Francisco Romero On

You can use an ArrayList<String>, like this:

Scanner s = new Scanner(new File(//Here the path of your file));

ArrayList<String> list = new ArrayList<String>();

while (s.hasNext())
{
    list.add(s.nextLine());
}

And if you want to get the value of some item of the ArrayList you just have to make reference with get function, like this:

list.get(//Here the position of the value in the ArrayList);

So, if you want to get all the values of the ArrayList you can use a loop to do it:

for (int i = 0; i < list.size(); i++)
{
   System.out.println(list.get(i));
}

And finally close your Scanner:

s.close();

I expect it will be helpful for you!

0
Spark-Beginner On

Assuming your one row in file contains one game only

 for (int i=0; input.hasNextLine(); i++) {
               String inputRecord = input.nextLine();
               input = new Scanner(inputRecord);

               String line=input.nextLine();

               arr[i]=line;
             }  
    return arr;