How to read and validate different portions of a line of text in a text file in Java? -
so i'm trying validate data in text file using java. text file looks (ignore bullet points):
- 51673 0 98.85
- 19438 5 95.00
- 00483 3 73.16
- p1905 1 85.61
- 80463 2 73.16
- 76049 4 63.48
- 34086 7 90.23
- 13157 0 54.34
- 24937 2 81.03
- 26511 1 74.16
- 20034 4 103.90
the first column of numbers needs within range of 00000-99999 , not letters, second column needs within range of 0-5, , third column needs within range of 0.00-100.00. how able validate each of these columns in text file separately meet requirements? know how read text file, i'm trying figure out how validate data.
so have line, string line = "20034 4 103.90";.
you can break consituent parts using .split().
then inspect/validate each of them individually before repeating same next line.
so, splitting delimiter " ", since separates columns.
string[] parts = line.split(" "); string part1 = parts[0]; // 20034 string part2 = parts[1]; // 4 string part3 = parts[2]; // 203.90 you can play around here http://ideone.com/lcnyq9
validation
regarding validation, it's quite easy.
- for column 1, can
if (i > 0 && < 100000) - same column 2,
if (i > 0 && < 6)
to check if column 1 doesn't contain letters, can use this:
part1.contains("[a-za-z]+") == false inside if statement.
Comments
Post a Comment