import java.io.*;
import java.util.*;
class Pgm4 {
public static void main(String[] args) throws IOException {
String[][] a = new String[24][24];
String lineIn, date, time, activity, userName, ipAddress, macAddress, bytes, packets;
int hours, minutes, seconds;
FileReader inputFile = null;
FileWriter outputFile = null;
inputFile = new FileReader("hours.dat");
outputFile = new FileWriter("InetHours.rpt");
BufferedReader br;
PrintWriter textFile = new PrintWriter(outputFile);
StringTokenizer st;
br = new BufferedReader(inputFile);
lineIn = br.readLine();
while ((lineIn = br.readLine()) != null) {
st = new StringTokenizer(lineIn);
date = st.nextToken();
time = st.nextToken();
activity = st.nextToken();
userName = st.nextToken();
ipAddress = st.nextToken();
macAddress= st.nextToken();
bytes = st.nextToken();
packets = st.nextToken();
textFile.println(date + " " + time + " " + activity + " " + userName + " " + ipAddress + " " + macAddress + " " + bytes + " " + packets);
}
inputFile.close();
outputFile.close();
}
}
Basically the only thing my code does at the moment is take the input file with the information and output it to a new file. I did this just so I could test it to make sure that it is reading all of the lines from the file and it is. I have to use a String Tokenizer to read each line of the file and then store each part of the file into a variable.
The trouble I am having is figuring out how I am going to go about storing the information about each user in my array. I have an idea of how I am going to calculate the time, but storing everything is bugging me. The main thing I need help with is figuring out how to check my array to see if the user is currently stored there. If the user isnt there then I need to figure out how to store the users information in the next available row. If the user is there then I need to stop on that row and do the calculations for the time and store the information back into the row for that user.
I appreciate any help you guys can give me on this. I have been tearing my hair out figuring out how to put this into code. I hope I have explained everything clearly. If not let me know and I can give you more details on what exactly I need to accomplish. Thank you guys so much!!!
Josh a.k.a. CyberGeek
[edited by - cybergeek on April 16, 2004 12:44:42 AM]
[edited by - cybergeek on April 16, 2004 12:46:04 AM]
🔒 Locked
Need help with a Java program
Advertisement
Original Post
Hi guys. I hope I am posting in the right forum. I need some help with a program I am trying to write for school. Basically the program takes a data file that lists the login and logout times of a user on the network and then calculates their total time spent on the network in a given month. Here is a sample of the data being read in:
2004-02-14 21:24:49 LOGIN fss324 192.168.1.195
2004-02-14 22:08:54 KICK fss324 192.168.1.195
2004-02-15 02:25:15 KICK fss223 192.168.1.200
2004-02-15 10:54:16 LOGIN fss223 192.168.1.200
2004-02-15 11:53:51 LOGOUT fss223 192.168.1.200
2004-02-15 16:09:23 LOGIN fss318 192.168.1.194
2004-02-15 16:32:55 KICK fss318 192.168.1.194
2004-02-15 19:10:45 LOGIN fss223 192.168.1.200
2004-02-15 20:54:18 LOGIN fss324 192.168.1.195
2004-02-15 21:49:23 KICK fss324 192.168.1.195
2004-02-16 02:30:46 KICK fss223 192.168.1.200
2004-02-16 05:28:29 LOGIN fss318 192.168.1.194
2004-02-16 06:06:32 LOGOUT fss318 192.168.1.194
The file contains the date, time of activity, whether the activity was a logon, logout, or kick, the username, and the ip address.
The printout will contain a row for each username with the following information for each user: number of logins, total time online, longest time online, shortest time online, and average time online.
The code that I have is as follows:
Thanks for your reply! NO I dont want to store that whole line in one cell. I want to store each piece into a cell. The array structure will look kind of like this:
Hopefully this gives you an idea of how it is going to look. EVen though it will not apepar on our printout, We are being required to read in the other information such as IP address in case there are entries where a user is logged in on more than one machine at a time. Then we can tell when there are two times that need to be added to the total by looking at the IP address.
My instructor told us that our array cannot be more than 24 x 24 and that we will not have more than 18 different users max in our file, so some of the rows can be used as temporary holding places for information. Hope this helps. If you need mroe clarified please let me know.
[edited by - cybergeek on April 17, 2004 1:22:34 AM]
0 1 2 3
0 username1 #of logins Total time online shortest time
1 username2
2 username3
3
Hopefully this gives you an idea of how it is going to look. EVen though it will not apepar on our printout, We are being required to read in the other information such as IP address in case there are entries where a user is logged in on more than one machine at a time. Then we can tell when there are two times that need to be added to the total by looking at the IP address.
My instructor told us that our array cannot be more than 24 x 24 and that we will not have more than 18 different users max in our file, so some of the rows can be used as temporary holding places for information. Hope this helps. If you need mroe clarified please let me know.
[edited by - cybergeek on April 17, 2004 1:22:34 AM]
1. Create two ints for using as an index for the array, lets call them indexR and IndexC.
2. Create two more ints, one to keep track of how many rows have been filled, lets call it "tracker". And the other lets call it "mover". Set both to 0.
3. Fill the entire first row of the array. (Called preparing the array)
4. Go to the next line in the text file.
5. Start a while loop.
6. Use a compareTo method to determine the new userName''s value compared to the userName that is at indexR. (Start checking at 0)
7.1 If new userName is less than the one at indexR, move the data at mover-1 to the row at mover. Then set mover-=1 and continue moving the values until you reach indexR, then insert the userName.
7.2 If new userName is equal to the one at indexR, do those calculations to modify your data.
7.3 If new userName is greater then the one at indexR, check the userName against indexR+1 and so forth.
8. If userName was not equal to any indexR, tracker+1. Repeat step 6 & 7 making sure to always set mover to tracker until the text file is done.
I''m sorry if im confusing (i tend to type that way) and for not giving you code, but it is for a class so that would be wierd...
If you need more specific help you can reach me on AIM at nicspizzico or email me at nicspizzico@aol.com
ps. I''m in a Java class too
2. Create two more ints, one to keep track of how many rows have been filled, lets call it "tracker". And the other lets call it "mover". Set both to 0.
3. Fill the entire first row of the array. (Called preparing the array)
4. Go to the next line in the text file.
5. Start a while loop.
6. Use a compareTo method to determine the new userName''s value compared to the userName that is at indexR. (Start checking at 0)
7.1 If new userName is less than the one at indexR, move the data at mover-1 to the row at mover. Then set mover-=1 and continue moving the values until you reach indexR, then insert the userName.
7.2 If new userName is equal to the one at indexR, do those calculations to modify your data.
7.3 If new userName is greater then the one at indexR, check the userName against indexR+1 and so forth.
8. If userName was not equal to any indexR, tracker+1. Repeat step 6 & 7 making sure to always set mover to tracker until the text file is done.
I''m sorry if im confusing (i tend to type that way) and for not giving you code, but it is for a class so that would be wierd...
If you need more specific help you can reach me on AIM at nicspizzico or email me at nicspizzico@aol.com
ps. I''m in a Java class too
Do you have to use arrays? I'd use a TreeMap to store the logins, and then another TreeMap to store you're data. A TreeMap is good because it's a binary tree, so then lookups are O(log(n)).
Here's how'd I do it...
When you get to a line that says LOGIN, add the user to the login Map. Then, when you get to a KICK or LOGOUT line, find the user in the login Map and calculate how long they've been logged on. After that, remove them from the login Map, check to see if they're in the data Map, and insert them into the data Map or add to the data that's already there. When using maps, make sure you remember that you can't have the same key twice, so you'll probably need a wrapper class in case the same user is connected with two computers. And if you don't know what a Map is, the Java API Specification might help.
In case you want to use arrays...
My array would be like this:
When you get to a login line, look for them in the array. They have to have the same name and IP. If there's no match, add them to the bottom (keep track of how many users are in the array). If there is a match, set their login time. When you come to a KICK or LOGOUT line, find the user with the same Name and IP in the array and calculate how long they've been online. Set their login time to 0, so that if you come across a stray LOGOUT, just chuck it. Now, when adding the data into the array, add it to the first occurrence of the same name, ignoring the IP. It might be a good idea remove the duplicate names that have a 0 login time (ignoring the first one of course).
Caeddyn
[edited by - Caeddyn on April 17, 2004 4:29:55 AM]
Here's how'd I do it...
When you get to a line that says LOGIN, add the user to the login Map. Then, when you get to a KICK or LOGOUT line, find the user in the login Map and calculate how long they've been logged on. After that, remove them from the login Map, check to see if they're in the data Map, and insert them into the data Map or add to the data that's already there. When using maps, make sure you remember that you can't have the same key twice, so you'll probably need a wrapper class in case the same user is connected with two computers. And if you don't know what a Map is, the Java API Specification might help.
In case you want to use arrays...
My array would be like this:
Name | IP Address | Login time | Total Time
Bob 192.168.0.1 2004-02-14 21:24:49 5h 27m 16s
Steeve 192.168.0.4 2004-02-14 21:31:54 36m 9s
Bob 192.168.0.2 2004-02-14 21:33:15 0s
Jimmy 192.168.0.1 0 2d 9s
John 192.168.0.3 2004-02-15 11:53:51 15s
When you get to a login line, look for them in the array. They have to have the same name and IP. If there's no match, add them to the bottom (keep track of how many users are in the array). If there is a match, set their login time. When you come to a KICK or LOGOUT line, find the user with the same Name and IP in the array and calculate how long they've been online. Set their login time to 0, so that if you come across a stray LOGOUT, just chuck it. Now, when adding the data into the array, add it to the first occurrence of the same name, ignoring the IP. It might be a good idea remove the duplicate names that have a 0 login time (ignoring the first one of course).
Caeddyn
[edited by - Caeddyn on April 17, 2004 4:29:55 AM]
This is how I''d do it:
- create a UserStats object with fields for total time, number of logins etc. It will also have a method handleEvent(int type, int time), where you have a type code for login/logout/kick. The handleEvent method will of course update the UserStats appropriately. It will also have a method print(), which returns a String representing a line of logfile output.
- Have a java.util.hashMap which maps user names (as Strings) to UserStats objects.
- Have a method updateUser(String name, int type, int time) which looks up the UserStats for that username and calls handleEvent on it. If the name is not found, create a new UserStats, handleEvent it and insert it in the hashMap.
- In the main loop, iteratively parse the time, type and user of the current event, and make an appropriate updateUser call. It looks like you can discard IP addresses entirely.
- Ask the hashmap for an Iterator over its values, i.e. the UserStats objects, and iteratively call print() on each value returned from the Iterator.next().
- create a UserStats object with fields for total time, number of logins etc. It will also have a method handleEvent(int type, int time), where you have a type code for login/logout/kick. The handleEvent method will of course update the UserStats appropriately. It will also have a method print(), which returns a String representing a line of logfile output.
- Have a java.util.hashMap which maps user names (as Strings) to UserStats objects.
- Have a method updateUser(String name, int type, int time) which looks up the UserStats for that username and calls handleEvent on it. If the name is not found, create a new UserStats, handleEvent it and insert it in the hashMap.
- In the main loop, iteratively parse the time, type and user of the current event, and make an appropriate updateUser call. It looks like you can discard IP addresses entirely.
- Ask the hashmap for an Iterator over its values, i.e. the UserStats objects, and iteratively call print() on each value returned from the Iterator.next().
Thank you all for replying with your help! I think I am going to try Caeddyn''s suggestion with the array first, since I have to use an array and we have not done anything with hashmaps or treemaps in the class.
I am having another problem though. I am trying to test things out to see how I will store the information in the array, but for some reason it is not working. For example, if I type
a[0][0] = userName;
System.out.println(a[0][0]);
the userName does not appear. It appears as NULL instead. What could be the problem there? Am I going to have to store data to the array in a different way? My instructor had told us that we cannot just stick the information in the array, that we would have to manipulate it somehow and then store it. This is part of whats bugging me because I cant figure out how.
Also, in regards to checking the array for the users, I havent been able to test anything since I cant get it to store any information in the first place, but would the best way be to use a for loop and iterate through the entire array looking for a username and ip address match?
Thank you guys for the help! My instructor refuses to help us with any logic questions outside of class. We have to ask them in class, and sometimes I cant always think of what I want to ask during class time. Your help is much appreciated.
I am having another problem though. I am trying to test things out to see how I will store the information in the array, but for some reason it is not working. For example, if I type
a[0][0] = userName;
System.out.println(a[0][0]);
the userName does not appear. It appears as NULL instead. What could be the problem there? Am I going to have to store data to the array in a different way? My instructor had told us that we cannot just stick the information in the array, that we would have to manipulate it somehow and then store it. This is part of whats bugging me because I cant figure out how.
Also, in regards to checking the array for the users, I havent been able to test anything since I cant get it to store any information in the first place, but would the best way be to use a for loop and iterate through the entire array looking for a username and ip address match?
Thank you guys for the help! My instructor refuses to help us with any logic questions outside of class. We have to ask them in class, and sometimes I cant always think of what I want to ask during class time. Your help is much appreciated.
Well my program is coming along pretty well now. I have figured out how to get information to store in the array and how to check to see if user names exist. If they dont exist, I am able to add them to the next available row.
I am having a different kind of problem now. It is related to the time calculations. Basically I have six separate variables for login hours, login minutes, login seconds, logout hours, logout minutes, and logout seconds. When a login or logout occurs, I use substrings to seperate the hours, minutes and seconds and then parse them into integers so I can use them in calculations. I then take the numbers after the calculations are finished and parse them back into strings and store them in my array.
My problem is this. For example, say a user logs on late at night and is on until early the next morning, I might have the following login and logout times:
23:11:05 Login time
02:05:13 Logout time
I was able to calculate on paper the total time online as being 2 hours 54 minutes and 8 seconds. For some reason however, I am having a brain fart and am unable to figure out how to figure up these calculations in my code. I think its the 24 hour clock that is getting me confused, but I cant get around it since the file uses this type of clock.
Can any of you guys give me some hints on how I should structure my code so that the computer can calculate the total time online? I am making progress, but these time calculations are confusing me. I''m sure its something simple. I just cant seem to figure it out. Thanks for the assistance!
I am having a different kind of problem now. It is related to the time calculations. Basically I have six separate variables for login hours, login minutes, login seconds, logout hours, logout minutes, and logout seconds. When a login or logout occurs, I use substrings to seperate the hours, minutes and seconds and then parse them into integers so I can use them in calculations. I then take the numbers after the calculations are finished and parse them back into strings and store them in my array.
My problem is this. For example, say a user logs on late at night and is on until early the next morning, I might have the following login and logout times:
23:11:05 Login time
02:05:13 Logout time
I was able to calculate on paper the total time online as being 2 hours 54 minutes and 8 seconds. For some reason however, I am having a brain fart and am unable to figure out how to figure up these calculations in my code. I think its the 24 hour clock that is getting me confused, but I cant get around it since the file uses this type of clock.
Can any of you guys give me some hints on how I should structure my code so that the computer can calculate the total time online? I am making progress, but these time calculations are confusing me. I''m sure its something simple. I just cant seem to figure it out. Thanks for the assistance!
Date and time manipulation is notoriously a pain in the ass. This is especially true in Java, since there aren''t any useful time or date facilities in the API to make this easier.
The trick is basically finding a good set of logic that works consistantly. Here''s a system I can think of:
1. Seconds: if the logout seconds are greater than the login seconds, then just do (logout - login). Otherwise, it''s 60 - (login - logout).
2. Minutes: same as seconds - either (logout - login) or 60 - (login - logout).
3. Hours: same idea as seconds and minutes. (logout - login - 1) if logout is greater, 24 - (login - logout) - 1 otherwise. The -1 is due to the fact that seconds and minutes act as "fractional" parts of time, while hours are more like the "whole" units of time.
This system continues on for days and months. Not years though, because the years don''t wrap. This logic came about simply through extensive observation, testing, etc. It should work pretty well, but I''ll leave it up to you to see if it works under all circumstances. Not tested, heh
The trick is basically finding a good set of logic that works consistantly. Here''s a system I can think of:
1. Seconds: if the logout seconds are greater than the login seconds, then just do (logout - login). Otherwise, it''s 60 - (login - logout).
2. Minutes: same as seconds - either (logout - login) or 60 - (login - logout).
3. Hours: same idea as seconds and minutes. (logout - login - 1) if logout is greater, 24 - (login - logout) - 1 otherwise. The -1 is due to the fact that seconds and minutes act as "fractional" parts of time, while hours are more like the "whole" units of time.
This system continues on for days and months. Not years though, because the years don''t wrap. This logic came about simply through extensive observation, testing, etc. It should work pretty well, but I''ll leave it up to you to see if it works under all circumstances. Not tested, heh
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement