Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checking file line's validity

 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "invalid" I mean not folowing the above format.
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic