Question

Good evening all. I'm just trying to create a class with a method that allows you to create a list of users, their ages, and their departments (although I haven't coded the department part yet!). I want to be able to return the list of employees SORTED by age. I haven't coded Java in a long while, and I'm a little rusty. Most of my time has been spent in Perl and C++ lately, so any help would be greatly appreciated. Thanks, guys!

Here's my (broken) code:

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
public class Department{

    public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("How many employees?\n");
    int noOfEmployees = in.nextInt();
    String[][] employeeInformation;
    employeeInformation = new String[noOfEmployees][3];



    for (int row = 0; row < employeeInformation.length; row++){
        System.out.print("Enter employee name: ");
        try {
            employeeInformation[0][row] = br.readLine();
            } catch (IOException e) {
            e.printStackTrace();
                }
        System.out.print("Enter employee age: ");
        try {
            employeeInformation[1][row] = br.readLine();
            } catch (IOException e) {
            e.printStackTrace();
                }
        }
     Arrays.sort(employeeInformation, new Comparator<String[]>() { 
           public int compare(String[] entry1, String[] entry2){
            String name = entry1[0];
            String age = entry2[0];
            return name.compareTo(age);
           }
            });
     for(int i=0;i<employeeInformation.length;i++){
         System.out.println(employeeInformation[i][i]);
     }
    }   
}
Was it helpful?

Solution 2

OK I have some thing for you. I took your class and modified it to make work and make little better. You may still want to make more error checking and other optimizations as you see fit. This is just a working sample.

Code explanation:
I have created a inner class called employee. But feel free to tear that out into a separate class. I just did for convenience sake to post a sample. I hope it helps you.I have tested and it works.

package com.vkg;

import java.util.Arrays;
import java.util.Scanner;

public class Department {

    public static void main(String[] args) {
        Department department = new Department();
        Scanner in = new Scanner(System.in);
        System.out.println("How many employees?\n");
        int noOfEmployees = in.nextInt();
        Employee[] employees = new Employee[noOfEmployees];

        for (int i = 0; i < employees.length; i++) {
            Employee employee = department.new Employee();
            System.out.print("Enter employee name: ");
            String employeeName = in.next();
            employee.setName(employeeName);

            System.out.print("Enter employee age: ");
            int age = in.nextInt();
            employee.setAge(age);

            employees[i] = employee;
        }
        Arrays.sort(employees);

        for (Employee employee : employees) {
            System.out.println(employee.getName() + "'s age = " + employee.getAge());
        }
    }

    public class Employee implements Comparable<Employee> {
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public int compareTo(Employee o) {
            if(this == o || this.age == o.age) {
                return 0;
            } else if (this.age < o.age) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}

OTHER TIPS

using your code

  employeeInformation[0][row] = br.readLine();
  employeeInformation[1][row] = br.readLine();

should be

  employeeInformation[row][0] = br.readLine();
  employeeInformation[row][1] = br.readLine();

and why are you comparing age with name in your sort

return name.compareTo(age);  // this does not make sense.

Sorry to be rough, but try using a debugger or print statements

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top