Gog1nA
Gog1nA

Reputation: 396

Java, sort and display data from txt file

i'm new with java and have some trouble with one task. i have txt file which looks like this:

John Doe,01-01-1980,Development,Senior Developer
Susan Smith,07-12-1983,Development,Head of Development
Ane Key,06-06-1989,BA,Junior Analyst
Nina Simone,21-09-1979,BA,Head of BA
Tom Popa,23-02-1982,Development,Developer
Tyrion Lannyster,17-03-1988,BA,Analyst

and i want to to sort it by departments. for example: Members are :

[Employee Full Name] - [Employee Age] - [Employee Position] - [Employee Salary(default value x)]

Deparment : Development Members are : Susan Smith ...... John Doe ...... Tom Popa ...... Department : BA Members are : Nina Simone ....... Ane Key ........... Tyrion Lannyster ........

at first read file and made 2d array but can't continue how to correctly sort it.

public static void main(String[] args) {

    String csvFile = "C:\\Employees.txt";
    BufferedReader br = null;
    String line = "";
    String SplitBy = ",";

    String myArray[][] = new String[6][5];
    int row = 0;

    try {
        br = new BufferedReader(new FileReader(csvFile));

        while ((line = br.readLine()) != null) {  

            String nums[] = line.split(SplitBy);

        for (int col = 0; col < nums.length; col++){

                String n =nums[col];

                myArray[row][col] = n;
           //     System.out.println(n);
            }               
            row++;



        }

              } 

    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}

Upvotes: 1

Views: 1102

Answers (3)

Pavan Kumar
Pavan Kumar

Reputation: 4810

Providing the code solution here would not help you learn it. But I can give you hints on how to proceed. Using array is not really recommended.

The easy, but dirty way -

Instead of two dimensional array, use a TreeMap<String, String[]> where key is the department concatenated with name and value is the one dimensional array of the individual's details. As we're using TreeMap, the result is naturally sorted based on Department followed by Name of the person. Loop through the entrySet and print all the results.

Right way -

Define new object Person with all the members needed. Implement Comparable interface. Read all the input data, populate the same in Person object, add each such objects in an ArrayList and use Collections API's sort method to sort all the objects. Alternatively you can adapt the Comparator way.

Upvotes: 1

Henrique Martins
Henrique Martins

Reputation: 352

The easiest way would put the contents of the lines in Java Beans and then sort them using sort.

public User { 
    private String name;
    // ... all the fields with getters and setters
}

Then adapt your code to something like this:

// create a nice List for the users.
List<User> userList = new ArrayList<>();

while ((line = br.readLine()) != null) {
            User user = new User();
            String nums[] = line.split(SplitBy);
            user.setName(nums[0]);
            // create nice method to convert String to Date
            user.setDate(convertStringToDate(nums[1]));
            // add the user to the list
            userList.add(user);

}

// Then finally sort the data according to the desired field.
Arrays.sort(userList, (a,b) -> a.name.compareTo(b.name));

Upvotes: 0

Aaron
Aaron

Reputation: 881

The Java Collections APIallows you to Sort as well as util.Arrays. You will need the arrays methods for you code, but consider moving to some sort of Collection. Perhaps a List to start with.

Upvotes: 0

Related Questions