-3

This is for my beginning Java class. There are similar questions asking to sort the values that are given in an array; I know how to do that, but here I need to read in a text file, and then sort the values and display them by employee name and the hours that they worked, while also keeping the order from most to least. This is what the text file looks like:

Jim 4 5 6 1 2 3 4
Harry 6 5 1 3 9 2 0
John 2 3 1 6 7 8 4
Lisa 2 1 5 4 1 2 6

And here is all that I know about reading in text files and my current code for this project.

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class EmployeeWorkHours {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    File file = new File("/Users/ODonnell/Desktop/inputData.txt");

    try {

        Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);

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

}
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51

3 Answers3

1

Parsing

Look at the Javadoc for java.util.Scanner, or use autocomplete in your IDE, you'll see a lot more methods than nextLine() etc, the ones of interest

  • hasNextInt() returns true when next token is a number
  • nextInt() the next integer

Storage

Now you need to store your numbers, I'd recommend a List as you won't know how many there are upfront which rules out primitive arrays.

  • Create a list with List hours = new ArrayList();
  • Add to it with add()

You'll also need to store your employees names, for simplicity I'd recommend using a Map of String to hours list, i.e. Map>.

  • Create with Map> employeeHours = new HashMap>()
  • Add to using employeeHours.put(name, hours)

Sorting

java.util.Collections.sort is all you need. This will sort your list by default in ascending order.

Displaying

Most if not all built in list implementations by default implement toString() so you can simply call System.out.println(hours)

Adam
  • 35,919
  • 9
  • 100
  • 137
0

You should save the hours worked and the names of your employees in a HashMap.

http://en.wikipedia.org/wiki/Hash_table For an explation of a HashMap.

After storing your values you can sort the HashMap like it is explained in the following link: Sort a Map<Key, Value> by values (Java)

Community
  • 1
  • 1
Deutro
  • 3,113
  • 4
  • 18
  • 26
0

You should use next() method on scanner instance to read next token instead of whole line. Then you have to try to parse it to integer to recognize if it is a name of employee or its work hour. In the for loop we are sorting data (using Collections utility class) and printing it.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class EmployeeWorkHours {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        File file = new File("inputData.txt");
        Map<String, List<Integer>> data = new HashMap<String, List<Integer>>();

        try {

            Scanner scanner = new Scanner(file);
            List<Integer> currentEmployee = null;

            while (scanner.hasNextLine()) {
                String token = scanner.next();
                try {
                    currentEmployee.add(new Integer(token));
                } catch (NumberFormatException e) {
                    currentEmployee = new ArrayList<Integer>();
                    data.put(token, currentEmployee);
                }
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        for (String name : data.keySet()) {
            Collections.sort(data.get(name));
            Collections.reverse(data.get(name));
            System.out.println(name + " " + data.get(name));
        }
    }
}
ERt
  • 26
  • 2