简体   繁体   English

如何在Java中读取和验证文本文件中文本行的不同部分?

[英]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. 因此,我正在尝试使用Java验证文本文件中的数据。 The text file looks like this (ignore the bullet points): 文本文件如下所示(忽略要点):

  • 51673 0 98.85 51673 0 98.85
  • 19438 5 95.00 19438 5 95.00
  • 00483 3 73.16 00483 3 73.16
  • P1905 1 85.61 P1905 1 85.61
  • 80463 2 73.16 80463 2 73.16
  • 76049 4 63.48 76049 4 63.48
  • 34086 7 90.23 34086 7 90.23
  • 13157 0 54.34 13157 0 54.34
  • 24937 2 81.03 24937 2 81.03
  • 26511 1 74.16 26511 1 74.16
  • 20034 4 103.90 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. 数字的第一列必须在00000-99999范围内且没有字母,第二列必须在0-5范围内,第三列必须在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"; 因此,您有一行, String line = "20034 4 103.90"; .

You can break it into its consituent parts using .split() . 您可以使用.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 您可以在这里玩耍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) 对于第1列,您可以执行以下操作: if (i > 0 && i < 100000)
  2. Same for column 2, if (i > 0 && i < 6) if (i > 0 && i < 6)if (i > 0 && i < 6)与列2相同

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. part1.contains("[a-zA-Z]+") == falseif语句中为part1.contains("[a-zA-Z]+") == false

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> : 步骤1:将文件中的每一行放入List<String>

List<String> list = Files.readAllLines(Paths.get("filepath"));

Step 2: Split each line into its components and validate them individually: 第2步:将每一行拆分成各个部分,并分别进行验证:

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. 您可以使用Scannerjavadocs )帮助您解析输入。 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. 当然,您可能会捕获任何潜在的异常并将其视为验证失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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