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
}
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.