Mykola Rudenko
Mykola Rudenko

Reputation: 314

How can I parse lines of data from file to populate objects for each?

I have a text file with the list inside, every line has data that I need insert in the new objects. So data looks like somename=3000 or another type with the slash data another type = 6000.

I have particular class "Item" that has String and int variable. Data need to be inserted into them. Every new object has to be added to the ArrayList<Item>.

// Calculate the lines for next for each loop
int lineCount = 0;
while (sc.hasNextLine()) {
    lineCount++;
    sc.nextLine();
}

for (int i = 0; i < lineCount; i++) {
    // creating the object
    Item item = new Item();
    // add item object to items ArrayList
    items.add(item);

    // add line to String variable lineToString,
    while (scaner.hasNextLine()) {
        String lineToString = scaner.nextLine();
        sc.nextLine();
    }

So, I figured out that to do this, I need to

I used Scanner to read a text file. When I try to insert the scaner.nextLine to the String it's doesn't work; I mean it's executing but variable String lineToString doesn't have the line from a text file.

Could somebody help with an idea of how better to proceed with this problem? Maybe there is some simpler way to insert the 2 different type of data from the text file line in the object and put it into the ArrayList? Every line in the text file has different data and has to be in different objects.

Upvotes: 1

Views: 1378

Answers (3)

Shafin Mahmud
Shafin Mahmud

Reputation: 4081

You didn't mention clearly the line format from the text file. I assume so far you have text file in which each line is like

someone=140000

And you are trying to read those lines of the text and parse each of them to an object of Item which contains a String property (I assume you name it name) and an int property (I assume you name it number)

If this is it, you fisrt need to read your text file line by line and process it further. There are several ways to read a text file line by line.

BufferReader

This is a very common and so far most appropriate way to read a text file in consider of performance.

List<Item> particulatItems = new ArrayList<>();

// using try-with-resource that will help closing BufferedReader by own
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       particularItems.add(processLine(line));
    }
}

Scanner

You could use Scanner too.

try (Scanner scanner = new Scanner(new File(fileName))) {
     while (scanner.hasNext()) {
        particularItems.add(processLine(line));
     }

} catch (FileNotFoundException e) {
   e.printStackTrace();
} 

You should extract the line processing logic to a independent function. This is good practice of clean coding.

public static Item processLine(Strinng line) {
     String[] tokens = line.split("=");
     Item item = new Item(tokens[0], tokens[1]);
}

Assuming you have that particular object defined as Item and you are populating a List of this type

 public class Item {
    String name;
    int number;

    public Item(String name, String numtxt) {
       this.name = name;
       this.number = Integer.parseInt(numtxt);
    }

   // setter getter
 }

More reading:

  1. How to read a large text file line by line using Java?
  2. Different ways of Reading a text file in Java
  3. Difference between Scanner vs BufferReader
  4. Try-with-resources in Java 7

Upvotes: 3

gagan singh
gagan singh

Reputation: 1611

Apart from what @Ashish Mishra mentioned, you are doing the second while loop in a for loop, why? Isn't one loop sufficient?

    int lineCount = 0;
    while (sc.hasNextLine()) {
        lineCount++;
        String lineToString = sc.nextLine();
        Item item = new Item();
        //convert lineToString to item
        items.add(item);
    }

Upvotes: 0

Ashish Mishra
Ashish Mishra

Reputation: 303

Looks like you have already scanned full file in below code snippet:

while (sc.hasNextLine()) {
    lineCount++;
    sc.nextLine();
}

After this, you are again iterating in for-loop but with same scanner, which has read last line So the following may return false:

while (scaner.hasNextLine()) 

I it may never enter while loop

You should reset scanner before iterating lines again .. or may be use something else than scanner to count lines

Upvotes: 0

Related Questions