简体   繁体   中英

How to read and validate different portions of a line of text in a text file in Java?

So I'm trying to validate data in a text file using Java. The text file looks like this (ignore the 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 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.

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

Validation

Regarding validation, it's quite easy.

  1. For column 1, you can do something like if (i > 0 && i < 100000)
  2. Same for column 2, 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.

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM