Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Selective retrieval of numbers in a text file

 
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the field separator between the columns?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
Eclipse IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may help you...
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it! Thanks
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic