Java 确定缺失数据

Java 确定缺失数据,java,Java,我有一个文本文件。它在每行中包含分隔数据,如 120 US 1 ALASKA 4. 某些行中缺少一些数据。像 US 1 ALASKA 4 因为它是由空格分隔的,所以我使用split来获取令牌。我无法确定丢失了哪些数据 在下面的代码中,示例input1将验证^[1-9]+$的120。如果行中缺少120,如示例input2,标记[0]将具有US,它将验证^[1-9]+$,并在“中给出”错误。与此相反,我想知道缺少哪个值。因此,我可以显示错误“error a i missing”,并且标记[1]应

我有一个文本文件。它在每行中包含分隔数据,如

120 US 1 ALASKA 4.
某些行中缺少一些数据。像

US 1 ALASKA 4
因为它是由空格分隔的,所以我使用split来获取令牌。我无法确定丢失了哪些数据

在下面的代码中,示例input1将验证
^[1-9]+$
120
。如果行中缺少
120
,如示例
input2
标记[0]
将具有
US
,它将验证
^[1-9]+$
,并在“中给出
”错误。与此相反,我想知道缺少哪个值。因此,我可以显示错误
“error a i missing”
,并且
标记[1]
应该包含
US
,以便正确验证

有人能给出一些做上述工作的想法吗

        String line = "120 US 1 ALASKA 4"; //Sample input1
       //String line = "US 1 ALASKA 4"; //Sample input2
        String delimiter = "[ ]+";
        String tokens[] = line.split(delimiter);

        String a = tokens[0];
        String b = tokens[1];
        String c = tokens[2];
        String d = tokens[3];
        String e = tokens[4];

        //Sample validation
        if(a.matches("^[1-9]+$")){
           System.out.println("Matches");
        }else{
           System.out.println("Error in a");
        }        

您可以像下面这样(考虑到您有四个元素),并且您的模式是数字字符串数字字符串

if (firstdata is not a number & firstdata is String) {
    //display alert as first data is missing
    // move the token data
    e=d;
    d=c;
    c=b;
    b=a;
}
对第二名数据执行同样的操作,等等

要确定缺少什么,您必须移动数据

更新1 下面是我的意思。。。。这是经过尝试和测试的代码,适用于您提供的两个输入

public class checkForPattern {
    public static void main(String[] args) {

        String line = "120 US 1 ALASKA 4"; // Sample input1
        // String line = "US 1 ALASKA 4"; //Sample input2
        String delimiter = "[ ]+";
        String tokens[] = line.split(delimiter);

        String a = "", b = "", c = "", d = "", e = "";
        try {
            a = tokens[0];
            b = tokens[1];
            c = tokens[2];
            d = tokens[3];
            e = tokens[4];
        } catch (Exception e1) {
        }

        System.out.println("String is : " + line);
        // Sample validation
        if (a.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
            System.out.println("Position First Matches.");
        } else {
            System.out.println("Error in first position.");
            e = d;
            d = c;
            c = b;
            b = a;
        }

        if (!(b.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+"))) {
            System.out.println("Position Two Matches.");
        } else {
            System.out.println("Error in second position.");
            e = d;
            d = c;
            c = b;
        }

        if (c.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
            System.out.println("Position Three Matches.");
        } else {
            System.out.println("Error in third position.");
            e = d;
            d = c;
        }

        if (!(d.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+"))) {
            System.out.println("Position Four Matches.");
        } else {
            System.out.println("Error in fourth position.");
            e = d;
        }

        if (e.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
            System.out.println("Position Five Matches.");
        } else {
            System.out.println("Error in fifth position.");
            e = d;
            d = c;
        }

    }
}

我会尝试以下方法:

  • 把一行分成记号
  • 分析每个标记以确定其可能属于的位置(字符串只能位于位置2或4,整数位于位置1、3或5。长字符串必须位于位置5
  • 由于令牌的位置必须增加,您可以确定可能缺少的令牌

    • 今天是星期六!让我们为您编写一个方法,假设模式是
      ,您可以更改模式。它很简单

      public static boolean validate(String line){
           String delimiter = "[\\s]+";
           String[] tokens = line.split(delimiter);
           int i=0;
           for(String s: tokens){
               switch (i) {
                  case 0:
                      if(!s.matches("\\d+")) throw new IllegalArgumentException("First element missing");
                      break;
      
                  case 1:
                      if(!s.matches("\\w{2}")) throw new IllegalArgumentException("2nd element missing");
                      break;
      
                  case 2:
                      if(!s.matches("\\d+")) throw new IllegalArgumentException("3rd element missing");
                      break;
      
                  case 3:
                      if(!s.matches("\\w+")) throw new IllegalArgumentException("4th element missing");
                      break;
      
                  case 4:
                      if(!s.matches("\\d+")) throw new IllegalArgumentException("5th element missing");
                      break;
      
              }
               i++;
           }
      
           if(i<4)throw new IllegalArgumentException((i+1)+" element missing");
           return true;
      }
      
      公共静态布尔验证(字符串行){
      字符串分隔符=“[\\s]+”;
      String[]tokens=line.split(分隔符);
      int i=0;
      for(字符串s:标记){
      开关(一){
      案例0:
      如果(!s.matches(“\\d+”)抛出新的IllegalArgumentException(“缺少第一个元素”);
      打破
      案例1:
      如果(!s.matches(“\\w{2}”))抛出新的IllegalArgumentException(“缺少第二个元素”);
      打破
      案例2:
      如果(!s.matches(“\\d+”)抛出新的IllegalArgumentException(“缺少第三个元素”);
      打破
      案例3:
      如果(!s.matches(“\\w+”)抛出新的IllegalArgumentException(“缺少第四个元素”);
      打破
      案例4:
      如果(!s.matches(“\\d+”)抛出新的IllegalArgumentException(“缺少第5个元素”);
      打破
      }
      i++;
      }
      
      如果(对于这样的语法,我认为使用
      Scanner
      )会做得更好(并且需要检查方法名称、参数类型等)


      如果其中一个字段丢失,那么您将在需要整数的位置获得一个非整数(因此会得到一个
      NoSuchElementException
      ),或者调用
      next()
      将返回错误的标记字符串,而
      equals
      测试将失败。

      您的模式是否总是
      ?@shiva0101 m:请参阅我的更新答案…代码正在为您请求的两个输入工作。。
          String line = "120 US 1 ALASKA 4";
          Scanner scanner = new Scanner(line);
          scanner.useDelimiter(" +"); // set the field delimiter regex.
          int value1, value2, value3;
      
      PARSE: 
          try {
              value1 = scanner.nextInt();
              if (!scanner.next().equals("USA")) {
                  ...
                  break PARSE; // or throw ...
              }
              value2 = scanner.nextInt();
              if (!scanner.next().equals("ALASKA")) {
                  ...
                  break PARSE; // or throw ...
              }
              value3 = scanner.nextInt();
          } catch (NoSuchElementException ex) {
              ...
          }