This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Selective retrieval of numbers in a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Selective retrieval of numbers in a text file" Watch "Selective retrieval of numbers in a text file" New topic
Author

Selective retrieval of numbers in a text file

Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

I have a text file as follows

I have to retrieve only those values which fall in the mem, swap and total under total, used and free i.e. I have to ignore 0, 564, 3237, 4074, 3848. How do I do it?

I have written the following code to display the all the numbers.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1474
    
    6

What is the field separator between the columns?


Swastik
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
1. You might want to read the text files line by line. Now you read it token by token using the next() method.
2. You have to check for matches in the lines read such as whether its for memory / swap / total.
3. Read only the first three integer tokens from the line being processed - but make sure always total used and free sizes are always present. Else you might end up reading shared, buffers, and even cached values.

Below piece of code might help you to build a scanner using the lines being read.



And make the lineScanner act as the myFile scanner you currently have. The lineScanner should act on all the tokens present in a single line.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Just an extra piece of information:
Do you have a control over the format of the file? Or is it always a text file.
If you have control over the file contents or the format- you might want to consider using xml/json format.


Mohamed Sanaulla | My Blog
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

Swastik Dey wrote:What is the field separator between the columns?

A couple of space.

John Jai wrote:
1. You might want to read the text files line by line. Now you read it token by token using the next() method.
2. You have to check for matches in the lines read such as whether its for memory / swap / total.
3. Read only the first three integer tokens from the line being processed - but make sure always total used and free sizes are always present. Else you might end up reading shared, buffers, and even cached values.

Thanks!! I will work on this and get back.

Mohamed Sanaulla wrote:Do you have a control over the format of the file? Or is it always a text file.
If you have control over the file contents or the format- you might want to consider using xml/json format.

I do have control over the file format. How is json going to help? I am very new to java.
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

Mohamed Sanaulla wrote:Just an extra piece of information:
Do you have a control over the format of the file? Or is it always a text file.
If you have control over the file contents or the format- you might want to consider using xml/json format.


As an extension to what I am doing currently, I have to be able to plot this into a graph (a pie chart or a bar graph). In that case, would json be more helpful than a textfile?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

The issue with the text file is that you may not be sure of the structure- there may be 4 spaces, or a tab or a new line seperating the data.

But with a structured document like XML or JSON you are for sure about the structure of the document and you can use parser api's already written to parse your XML or JSON documents.
rastogi payam
Ranch Hand

Joined: May 09, 2011
Posts: 45

This may help you...


SCJP 1.6, OCPWCD 5
Shikha Upadhyaya
Ranch Hand

Joined: Aug 17, 2011
Posts: 70

Got it! Thanks
 
 
subject: Selective retrieval of numbers in a text file