populating an array with a text file

text file, arrays and count

Page 1 of 1

10 Replies - 5152 Views - Last Post: 07 June 2009 - 04:18 PM Rate Topic: -----

#1 lebouffonvert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-February 08

populating an array with a text file

Post icon  Posted 05 June 2009 - 08:47 PM

Good evening :) ,

I am fairly new at java, and would like to request your help.
Here is my goal, I have a text file with log information that is not consistent.
example log file content:
mai 6 03:26:53 macbookscuba Dock[215]: Corrupt JPEG data: premature end of data segment
mai 6 03:26:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]:
Tue mai 6 03:26:53 macbookscuba.local Dock[215] <Error>: Corrupt JPEG data: premature end of data segment
mai 6 09:28:51 localhost com.apple.kextd[10]: __ZN16IO80211Interface11postMessageEi
mai 6 09:28:51 localhost com.apple.kextd[10]: __ZN16IO80211Interface22monitorModeInputPacketEP6__mbuf
mai 6 09:28:51 localhost com.apple.kextd[10]: __ZN17IO80211Controller20outputRaw80211PacketEP16IO80211InterfaceP6__mbuf
mai 6 09:28:51 localhost kernel[0]: Previous Shutdown Cause: -60
mai 6 09:28:51 localhost kextd[16]: kld_load_from_memory() failed for module /System/Library/Extensions/IO80211Family.kext/Contents/PlugIns/AirPortAtheros5424.kext/Contents/MacOS /AirPortAtheros5424

I would like to Parse this text file and obtain statistics for:
- Number of messages per minute.
- Number of messages per hour.
- Number of messages per day.

As you probably noticed there is a "Tue" that randomly shows up before my date.

What I was thinking of doing was taking the content of hte text file, and placing it in an array, then count the number of times I see mai 6, then the number of hours, then the number of times I see the same minutes.

unfortunatly I have to admit I am a little lost on how to go obtaining my goal. And my result is not what I wish, furthermore I do not know how to go about breaking down the hour/minute/secondes?

Here is what I have so far:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class ArrayTest
{

	public static void main(String[] arg) 
	{
		String[] month = new String[100];
		String[] day = new String[100];
		String[] time = new String[100];
		String[] appName = new String[100];
		String[] port = new String[100];
		String[] message = new String[100];
		
		int counter = 0;
		try {
			Scanner input = new Scanner(new File("data.txt"));
			while(input.hasNext()) {
				month[counter] = input.next();
				day [counter] = input.next();
				time[counter] = input.next();
				appName[counter] = input.next();
				port[counter] = input.next();
				message[counter] = input.next();
				
				System.out.println(counter + " month" + month[counter] + " day:" + day[counter] + " time:" + time[counter] + " appName:" + appName[counter] + " port:" + port[counter] + " message:" + message[counter]); 
				counter++;
			}
		} catch (FileNotFoundException e) {
		}
	}
}



Thank you in advance to point me in the right direction,

John

Is This A Good Question/Topic? 0
  • +

Replies To: populating an array with a text file

#2 jpete7683  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 80
  • Joined: 18-April 09

Re: populating an array with a text file

Posted 05 June 2009 - 09:33 PM

From looking at your code you are wanting to read the full file into each array and then sort the array for the information you are wanting? Is this correct?

I would think it could be done easier if you used a for loop and then nested if statements inside of the for loop to add to a specific array depending of the contents of the line read in.
Was This Post Helpful? 0
  • +
  • -

#3 lebouffonvert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-February 08

Re: populating an array with a text file

Posted 06 June 2009 - 10:06 AM

View Postjpete7683, on 5 Jun, 2009 - 08:33 PM, said:

From looking at your code you are wanting to read the full file into each array and then sort the array for the information you are wanting? Is this correct?

I would think it could be done easier if you used a for loop and then nested if statements inside of the for loop to add to a specific array depending of the contents of the line read in.


yes it is correct I'm trying to populate the whole array.
Thank you for your response, I'm currently looking on how to implement your suggestion.

John

This post has been edited by lebouffonvert: 06 June 2009 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 lebouffonvert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-February 08

Re: populating an array with a text file

Posted 06 June 2009 - 11:21 AM

View Postlebouffonvert, on 6 Jun, 2009 - 09:06 AM, said:

View Postjpete7683, on 5 Jun, 2009 - 08:33 PM, said:

From looking at your code you are wanting to read the full file into each array and then sort the array for the information you are wanting? Is this correct?

I would think it could be done easier if you used a for loop and then nested if statements inside of the for loop to add to a specific array depending of the contents of the line read in.


yes it is correct I'm trying to populate the whole array.
Thank you for your response, I'm currently looking on how to implement your suggestion.

John



View Postlebouffonvert, on 6 Jun, 2009 - 10:20 AM, said:

View Postlebouffonvert, on 6 Jun, 2009 - 09:06 AM, said:

View Postjpete7683, on 5 Jun, 2009 - 08:33 PM, said:

From looking at your code you are wanting to read the full file into each array and then sort the array for the information you are wanting? Is this correct?

I would think it could be done easier if you used a for loop and then nested if statements inside of the for loop to add to a specific array depending of the contents of the line read in.


yes it is correct I'm trying to populate the whole array.
Thank you for your response, I'm currently looking on how to implement your suggestion.

John

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,353
  • Joined: 20-September 08

Re: populating an array with a text file

Posted 06 June 2009 - 01:30 PM

Quote

As you probably noticed there is a "Tue" that randomly shows up before my date.


Yes - rather strange - there seems to be a mix of English and another language
Was This Post Helpful? 0
  • +
  • -

#6 lebouffonvert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-February 08

Re: populating an array with a text file

Posted 06 June 2009 - 02:49 PM

View Postg00se, on 6 Jun, 2009 - 12:30 PM, said:

Quote

As you probably noticed there is a "Tue" that randomly shows up before my date.


Yes - rather strange - there seems to be a mix of English and another language


I've been looking at the suggested solution approach of doing loops and upon certain conditions to place the record in the array but I am not really sure how to go about that; and to also take in consideration that “tue” which doesn’t always occur. Would somebody have an example or perhaps a tutorial I could refer to please.
Thanks,
John
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,353
  • Joined: 20-September 08

Re: populating an array with a text file

Posted 06 June 2009 - 02:52 PM

Well, are we to infer that something like 'Wed' or 'Thu' might occur instead?
Was This Post Helpful? 0
  • +
  • -

#8 lebouffonvert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-February 08

Re: populating an array with a text file

Posted 06 June 2009 - 02:56 PM

View Postg00se, on 6 Jun, 2009 - 01:52 PM, said:

Well, are we to infer that something like 'Wed' or 'Thu' might occur instead?

I would keep that possibility in mind, however the particular text file I am working with only is for 1 day of data therefore for this case only "tue".
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,353
  • Joined: 20-September 08

Re: populating an array with a text file

Posted 06 June 2009 - 03:11 PM

Attaching a full example log file might help. I'd be inclined to use a Map where the keys are derived using regex
Was This Post Helpful? 0
  • +
  • -

#10 lebouffonvert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-February 08

Re: populating an array with a text file

Posted 06 June 2009 - 03:18 PM

View Postg00se, on 6 Jun, 2009 - 02:11 PM, said:

Attaching a full example log file might help. I'd be inclined to use a Map where the keys are derived using regex


Here is the full log. I'm in the middle of giving food to the kids to so my responses may be a little slow :)

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#11 lebouffonvert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-February 08

Re: populating an array with a text file

Posted 07 June 2009 - 04:18 PM

View Postlebouffonvert, on 6 Jun, 2009 - 02:18 PM, said:

View Postg00se, on 6 Jun, 2009 - 02:11 PM, said:

Attaching a full example log file might help. I'd be inclined to use a Map where the keys are derived using regex


Here is the full log. I'm in the middle of giving food to the kids to so my responses may be a little slow :)


Would you, please, happen to perhaps have an example or a tutorial of "Map where the keys are derived using regex"?

Thanks
John

This post has been edited by lebouffonvert: 07 June 2009 - 04:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1