Reputation: 81
So I'm trying to validate data in a text file using Java. The text file looks like this (ignore the bullet points):
The first column of numbers needs to be within the range of 00000-99999 and with not letters, the second column needs to be within the range of 0-5, and the third column needs to be within the range of 0.00-100.00. So how I would be able to validate each of these columns in the text file separately to meet the requirements? I already know how to read the text file, I'm just trying to figure out how to validate the data.
Upvotes: 2
Views: 901
Reputation: 3121
You can use Scanner
(javadocs) to help you parse the input. It's similar to the regular expressions solution but it's tailored for these situations where you read a series of values from a potentially enormous text file.
try (Scanner sc = new Scanner(new File(...))) {
while (sc.hasNext()) {
int first = sc.nextInt();
int second = sc.nextInt();
float third = sc.nextFloat();
String tail = sc.nextLine();
// validate ranges
// validate tail is empty
}
}
Off course you may catch any potential exceptions and consider them as validation failures.
Upvotes: 0
Reputation: 1636
Instead of checking if it doesn't have letters, check that it only contains digits or decimal points. I've provided the appropriate regular expressions for doing the same.
Step 1: Put each line in the file into a List<String>
:
List<String> list = Files.readAllLines(Paths.get("filepath"));
Step 2: Split each line into its components and validate them individually:
for(String str : list)
{
String[] arr = list.split(" ");
if(arr[0].matches("\\d+")) // Check to see if it only contains digits
int part1 = Integer.valueOf(arr[0]);
else
//throw appropriate exception
validate(part1, minAcceptedValue, maxAcceptedValue);
if(arr[1].matches("\\d+")) // Check to see if it only contains digits
int part2 = Integer.valueOf(arr[1]);
else
//throw appropriate exception
validate(part2, minAcceptedValue, maxAcceptedValue);
if(arr[2].matches("[0-9]{1,4}(\\.[0-9]*)?")) // Check to see if it is a Double that has maximum 4 digits before decimal point. You can change this to any value you like.
int part2 = Integer.valueOf(arr[2]);
else
//throw appropriate exception
validate(part3, minAcceptedValue, maxAcceptedValue);
}
void validate(int x, int min, int max)
{
if(x < min || x > max)
//throw appropriate exception
}
Upvotes: 1
Reputation: 15091
So you have a line, String line = "20034 4 103.90";
.
You can break it into its consituent parts using .split()
.
Then inspect/validate each of them individually before repeating the same for the next line.
So, it would be splitting by the delimiter " "
, since it separates the 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
Regarding validation, it's quite easy.
if (i > 0 && i < 100000)
if (i > 0 && i < 6)
To check if the column 1 doesn't contain any letters, you can use this:
part1.contains("[a-zA-Z]+") == false
inside an if
statement.
Upvotes: 3