0

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]);
     }
    }   
}
IAdapter
  • 62,595
  • 73
  • 179
  • 242
user3527961
  • 35
  • 2
  • 5
  • 1
    so what is broken? Help us to help you. – Scary Wombat May 02 '14 at 01:28
  • Sorry! Here's my current output after sort: How many employees? 2 Enter employee name: chris Enter employee age: 22 Enter employee name: ben Enter employee age: 21 22 ben – user3527961 May 02 '14 at 01:34
  • Be clear in your problem. From your code, I doubt if you have ever actually code in Java, or even code seriously: `return name.compareTo(age);` this problem is so obvious isn't it? Learn to write a proper class, and write small programs to learn to 1. read data from stdin, and 2. do sorting. – Adrian Shum May 02 '14 at 01:59

2 Answers2

0

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

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
-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;
            }
        }
    }
}
vkg
  • 1,839
  • 14
  • 15
  • not the downvoter, but this does not fix **his** code – Scary Wombat May 02 '14 at 01:52
  • What is still broke? Have tried to run this? I tested the output and it works. Can the DOWNVOTER please explain the reason for downvoting? Just curious? – vkg May 02 '14 at 01:54
  • 1
    I think the idea is to _help_ the poster with his code, not do his work for him. – awksp May 02 '14 at 02:16
  • Is that the reason somebody downvoted me? But keep in mind different users need different level of help based on their expertise. And I think idea is to help everyone here isn't it? – vkg May 02 '14 at 02:25
  • I'm not sure who downvoted, but thanks for the guidance. I actually have been coding for a long while now, but I'm trying to pick up where I left off on Java. The code I posted in the OP was what I had come up with over digging through some old code and textbooks. Thanks again, I'll work with this and come up with something shiny! I should also add, the absolute joke of a 2D array I posted was the result of me screwing with things, not an actual completed code at all. I should have been more clear that I just needed guidance. – user3527961 May 02 '14 at 03:38
  • If you like what I posted please accept it as a answer. I was honestly trying to help. – vkg May 02 '14 at 03:58
  • I got distracted trying to upvote, but I don't have enough rep yet. ;) – user3527961 May 02 '14 at 04:13