0

I am trying to write the data from a Text file into an ArrayList. I have the code below. The lines in the text file have this format:

Id
Car Manufacturer
Car Type
Colour
*blank line*

For Example:

21
Vauxhall
Corsa
red

19
Vauxhall
Corsa
blue

18
Vauxhall
Corsa
White

It does this 24 times, displays the information correctly when run, but the size of the list is "1". I was wondering if it is possible to split up each car by the blank space so they can be individually separated.

5
  • The blank space is an empty line, right? Commented Apr 21, 2016 at 11:43
  • 2
    Please provide a sample of the file contents, currently it's hard to tell what your problem is. As an example you define the delimiter to be ,\s*, i.e. a comma followed by any whitespace, but your description doesn't mention that comma. Commented Apr 21, 2016 at 11:44
  • 1
    I don't understand your file format. Do you store id ,car manufacturer and so on in one line for each car or in separate lines? Commented Apr 21, 2016 at 11:46
  • 1
    Maybe you can use this, if blank space is an empty line. Commented Apr 21, 2016 at 11:47
  • @thomas i have updated Commented Apr 21, 2016 at 11:50

5 Answers 5

2

You are currently only reading in the file in a value by value basis. It might be worth considering changing to reading the file in a line by line basis.

So you could use the method like so:

String lineValue = "";

while (inFile1.hasNextLine()) {
  lineValue = inFile1.nextLine();

  if(lineValue.isEmpty()) {
    temps.add(token1);
    token1 = "";
  } else {
    token1 += lineValue;
  }
}

This would allow it to add multiple separate strings to the list. You may need to include a blank space at the end of the file to add the last string though, or add an extra bit of code to check if it's at the end of the file.

Sign up to request clarification or add additional context in comments.

Comments

1

I hope, that this code will rsolve your problem:

public static void main(String[] args) throws IOException {
    FileReader inFile1 = new FileReader(new File("example.txt"));
    List<String> temps = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(inFile1);
    String line = "";
    String propertyText = "";
    while ((line = reader.readLine()) != null) {
        if(line.length() > 0) {
          propertyText += line + "\n";
        }else{
            temps.add(propertyText);
            propertyText = "";
        }
    }
    temps.add(propertyText);
    inFile1.close();

    String[] tempsArray = temps.toArray(new String[0]);

    for (String s : tempsArray) {
        System.out.println(s);
    }

    // this will print the size of this list
    int retval = temps.size();
    System.out.println("Size of list = " + retval);
}

1 Comment

Thank you! this is exactly what i'm looking for.
1

Agree with @Thomas. Follow the code:

        Scanner inFile1 = new Scanner(new File("example.txt")); // don't use any delimiter 

        List<String> temps = new ArrayList<String>();
        String tokenTemp= "";


        while (inFile1.hasNextLine()) {
        tokenTemp = inFile1.nextLine();
        if(tokenTemp.isEmpty()){
            temps.add(token1);
            token1="";
        }
        else token1 += tokenTemp+" ";
    }

Here every line inserted in temps list.

3 Comments

This is the right step I am looking for. But I am wondering if it is possible to make it so each car is stored separately. So from the ID to the Colour.
Sorry I accidentally posted the last comment too early. I have edited it accordingly.
Plz, see the update. i wasn't check your last update for your sample input.
0

This is because the entire file is read at once due to the regex. I modified your code like this,

Scanner inFile1 = new Scanner(new File("example.txt"));
            //.useDelimiter(",\\s*");
List<String> temps = new ArrayList<String>();
while (inFile1.hasNext()) {
    token1 = inFile1.nextLine();
    temps.add(token1);
}

And it works. If every new value is printed in a new line, you can use nextLine() method of Scanner class.

Comments

0

From what You have posted, I understand that you differentiate the details of each car with a blank line, you can you the following regex pattern to split whenever two new line characters occur (one for the end of line and one for the blank line)

20
Vauxhall
Corsa
red (newline1)
(newline2)
19
Vauxhall
Corsa
blue(newline1)
(newline2)
18
Vauxhall
Corsa
White

This should do it:

Scanner inFile1 = new Scanner(new File("cars.txt")).useDelimiter("(\r\n){2,}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.