Note that there are some explanatory texts on larger screens.

plurals
  1. POjava: read from text file and make objects of it and add to collections and then sort
    primarykey
    data
    text
    <p>What i want to do is:</p> <ol> <li>Create an Employee class having private fields as mentioned in any of the file.</li> <li>Read the content from the files(there are 150 files) in the folder and create employee object for each file.</li> <li>Store the Employee objects created in a Collection</li> <li>Create methods which would sort the employee collection according to the different fields in ascending and descending order.</li> </ol> <p>And my code for the 1st 3 lines is:</p> <pre><code>package com.fulcrum.emp; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class TestingColections { public static void main(String[] args) { File folder = new File("D:\\employee files"); File[] listOfFiles = folder.listFiles(); ArrayList&lt;Employee&gt; emp = null; int id = 0; String name = null; int age = 0; for (File file : listOfFiles) { try { Scanner scanner = new Scanner(file); String tokens = ""; String[] newtokens = null; while (scanner.hasNext()) { tokens = tokens.concat(scanner.nextLine()).concat(" "); tokens = tokens.replace("=", "|"); newtokens = tokens.split("[|\\s]"); } id = Integer.parseInt(newtokens[1]); name = (newtokens[3] + " " + newtokens[4]); age = Integer.parseInt(newtokens[6]); emp = new ArrayList&lt;Employee&gt;(); emp.add(new Employee(id, name, age)); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } </code></pre> <p>My problem is instead of just <code>add(</code>) I tried to add object in a specified index using for loop but it gave <code>IndexOutOfBoundException</code>.<br>How can i do that?can anyone please help?<br> And also I want to sort those abjects according to different field.<br>Please guide me on that too.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload