It's not a secret anymore!*
The moose likes Beginning Java and the fly likes checking file line's validity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "checking file line Watch "checking file line New topic
Author

checking file line's validity

kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
I am reading from a file, and storing it into an array. Each line must have the format of

SSN|first|last|grade\humCredits|gpa

If there a student with invalid info, I am supposed to reject it and store the rest into the array.

It must have a boolean return value. I have written a basic definition, but I can't see to figure how to reject invalid text fle line, and get to the next line.

Any ideas?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

It depends on what you mean by "invalid".
I would be tempted to use String.split() method and check the size of the returned array.


[My Blog]
All roads lead to JavaRanch
kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
By "invalid" I mean not folowing the above format.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

Originally posted by kenji mapes:
By "invalid" I mean not folowing the above format.


Satou was probably referring to the specifics of the above format. For example, is SSN a number or can it contain letters? if number, what is the valid range? Can the first and last name contain numbers? What is a valid grade? etc.

Anyway, if all you are concerned with is the number of fields, that are separated by a specific delimiter, then probably using split() is best -- as you also want an array of the components.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Sorry, it is still unclear. Looking at the above format does not explain the requirements.
Do you mean :
1. Each items must be separated by a "|"
2. grade and humCredits must be separated by a "\"

If so, you can try String.split(). Here is a sample for number 1:
kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
Sorry the / was a typo.

I have two txt files to process:

One is bad and one is good.


Good one:

123456789|Smith|John|freshman|15|4.0
987654321|Doe|Joe|sophomore|35|3.9
846834676|Wang|Chin|senior|95|3.7
957654321|Russell|June|junior|75|3.5
957654320|Russell|Harold|senior|195|3.2
456789321|Ortiz|Juan|freshman|25|3.8
987123456|Wade|Warren|sophomore|56|2.5
578315853|Ellis|Anita|sophomore|45|3.8

Bad one:

123456789|Smith|John|freshman|15|4.0
987654321|Doe|Joe|sophomore|35|3.9
248615937|Doe|Jane|senior|35|3.9
846834676|Wang|Chin|senior|95|3.7
9jtd85765|Duval|Janice|senior|100|3.7
957654321|Russell|June|junior|75|3.5
957654320|Russell|Harold|senior|195|3.2
456789321|Ortiz|Juan|freshman|25|3.8
519372486|Fields|Alice
987123456|Wade|Warren|sophomore|56|2.5
578315853|Ellis|Anita|sophomore|45|3.8


So I was not given too much criteria on what to check for, only to check the "format." So, from the providd files, it appears an invallid format, would be one without all the proper field regardless of the characters, etc.

So how would I check? Maybe number of delimeters? i was also thinking that I could put it into the array, and then just delete it?

thanks for your help guys.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

So how would I check? Maybe number of delimeters? i was also thinking that I could put it into the array, and then just delete it?


Please refer to the sample I have posted then.
It will return an array. You can then check the number of items in it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: checking file line's validity
 
Similar Threads
Incompatible type issue
B&S 2.2.2 updateRecord()
please help with tokenizing
URLyBird 1.2.1 - How to handle "the magic cookie value" ?
Problem with arrays. StringTokenizers (null pointer)