My program simulates the operation of a lift(elevator) and it creates a line in a text file every time a 'user' uses the lift.
Sample of the text file:
25/03/14_05:09:24 Slccj Azeepc 1 2
25/03/14_05:37:48 Wfvp Dvjhlwvpc 3 5
25/03/14_05:38:27 Mfkk Wtrsejplc 2 1
25/03/14_05:39:00 Qczoz Mlrrtyd 0 4
Each usage creates a line in the file with the timestamp, the (encoded) user's name, the floor which they entered the lift and the floor alighted on.
I've been trying to figure out how to print a report which details how many times each user has used the lift in descending order.
Here is what I have so far:
public void showUseCount2() {
int[] userCount = new int[4];
String[] users = new String[4];
users[0] = "Wfvp Dvjhlwvpc";
users[1] = "Qczoz Mlrrtyd";
users[2] = "Slccj Azeepc";
users[3] = "Mfkk Wtrsejplc";
try {
String strLine;
for (int i = 0; i < userCount.length; i++) {
FileInputStream fis = new FileInputStream("reports.txt");
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
while ((strLine = br.readLine()) != null) {
int startIndex = strLine.indexOf(users[i]);
while (startIndex != -1) {
userCount[i]++;
startIndex = strLine.indexOf(users[i], startIndex + users[i].length());
}
}
dis.close();
}
Arrays.sort(userCount);
System.out.println(Arrays.asList(userCount));
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println("Wfvp Dvjhlwvpc used the lift: " + userCount[0] + " times");
System.out.println("Qczoz Mlrrtyd used the lift: " + userCount[1] + " times");
System.out.println("Slccj Azeepc used the lift: " + userCount[2] + " times");
System.out.println("Mfkk Wtrsejplc used the lift: " + userCount[3] + " times");
}
This is the output I'm getting at the moment:
[[I@19b04e2]
Wfvp Dvjhlwvpc used the lift: 1 times
Qczoz Mlrrtyd used the lift: 1 times
Slccj Azeepc used the lift: 1 times
Mfkk Wtrsejplc used the lift: 2 times
So my for loop successfully fills the userCount array. I've tried using Arrays.sort but it doesn't seem to be working properly for me. So I need a way to go about sorting these numbers in the array whilst maintaining a relationship between the user and their use count. Any help will be appreciated!
System.out.println(Arrays.toString(userCount));