0

Say that I have a textfile that Im looping through each line in. The text files lines look like this:

1
2
3
4

1
2
3
4

1
2
3
4

I also have a class called DataHolder that I want a new instance of with each segment (where a segment is the lines 1 2 3 4). The DataHolder class has variables for 1 2 3 and 4. When the iterator hits the whitespace, a new object of the DataHolder class should be created for the next 1 2 3 4.

How can I accomplish this ? This is what I have at the moment

File theFile = new File(pathToFile);
try
{
    Scanner fileContent = new Scanner(theFile);
    DataHolder data = new DataHolder();
    while(fileContent.hasNextLine())
    {
        String line = fileContent.nextLine();
        if(line == "")
        {

        }
    }  
}
catch(Exception e)
{
    // ToDo
}
2
  • Read each line. If value is not empty, store the value into a List. When you hit the empty space, create the DataHolder pulling the values from the List. Then clean out the List and repeat Commented Feb 4, 2015 at 20:15
  • new DataHolder() instantiates a new object. You can perform such an action inside your loop as well as out. Of course, you need to do something with the new instance (assign it to a variable, put it in a collection, ...) for that to be any use. Commented Feb 4, 2015 at 20:15

2 Answers 2

3
File theFile = new File(pathToFile);
try
{
    Scanner fileContent = new Scanner(theFile);
    List<DataHolder> dataList = new ArrayList<DataHolder>();
    List<String> stringList = new ArrayList<String>();
    while(fileContent.hasNextLine())
    {
        stringList.add(fileContent.nextLine());
        if(line.equals(""))
        {
            if (!stringList.isEmpty())
            dataList.add(new DataHolder(stringList));
            stringList.clear();

        }
    }  
}
catch(Exception e)
{
    // ToDo
}
Sign up to request clarification or add additional context in comments.

2 Comments

While Brian's answer is good, this is the completed code snippet.
Just a minor comment, you could close the resources.
3

This:

if(line == "")

is incorrect since you're comparing references. You should use

if(line.equals(""))

Then you can reliably compare strings, and then re-initialise your DataHolder object in the loop via:

if(line.equals("")) {
   data = new DataHolder();
}

Presumably you need to store a collection of DataHolders, in which case you would add it to the collection here. Perhaps also investigate String.trim(), such that multiple whitespaces don't break your parsing.

3 Comments

This only provides part of the answer. This doesn't set the variables in DataHolder
It's not what he's asked for (despite that section being missing in the code)
It seems like that is what they are asking: The DataHolder class has variables for 1 2 3 and 4. and then DataHolder class should be created for the next 1 2 3 4.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.