• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Validate each line of a file

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I have a file with some lines and I have to validate each line by retrieving a number in these lines.

I'm using the Scanner class to retrieve my lines, but when I need to retrieve the number (in position 80 by example), i don't have an other idea than to use the substring method, someone has a better idea? That's not very beautiful ;)

Thanks in advance!
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it ALWAYS in position 80?

You can also look into tokenizing your string, which may or may not work. That would assume the data is always in the Xth token.

Or you may have to look for some other marker, like "score: ", and then get the characters after that up to some other character, like a semi-colon...

This is where programming becomes more of an art than a science...you need to analyze your data and figure out where the piece you want will be, and then determine how to get it. So I'd say first start by analyzing your data, and come up with an algorithm for finding the data you need in the line.
 
Greenhorn
Posts: 21
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also use charAt() method of String class provided that the position is known.
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks!

An other question about Scanner, there's a way to know the previous line, the current line and the next line?
 
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fabian Angy wrote: . . . An other question about Scanner, there's a way to know the previous line, the current line and the next line?

The only way to know the next line is clairvoyance

You are going to have to store them. You could write yourself a List class which has a fixed size and automatically deletes the oldest member. Or a Queue might do the same. There might be something already available: try Apache Commons if you can't find anything in the Sun/Oracle API. Or try a 3-member String[] array.
 
reply
    Bookmark Topic Watch Topic
  • New Topic