java - 无效数据帮助我

标签 java

我正在尝试解析我的代码,以便将无效数据编辑为“数据无效”之类的内容。我在使分离器工作时遇到困难。

单词一 - 单词二 - 单词三 - 单词四 - 单词五 有没有更简单的方法来做到这一点,而不是将每个单独的标记排序到数组中。因此,在“-”附加到单词的地方,我想了解如何更改它并显示“无效数据”之类的内容

最佳答案

只需围绕“-”进行拆分,因为这就是分隔数据的方式。每当您在数组中获得一个空格作为索引时,您就知道该数据不包含该数据。请尝试此操作,检查数据是否不是空格或数据是否与数字匹配。

  while(read.hasNextLine())
  {
      line = read.nextLine();
      String[] words = line.split("-");
      if(words.length == 5)
      {
          Title[counter] = words[0].matches("\\s+") ? "No Title" : words[0];
          Author[counter] = words[1].matches("\\s+") ? "No Author" : words[1];
          Price[counter] = !words[2].matches("\\d+") ? 0 : Integer.parseInt(words[2]);
          Publisher[counter] = words[3].matches("\\s+") ? "No Publisher" : words[3];
          ISBN[counter] = !words[4].matches("\\d+") ? 0 : Integer.parseInt(words[4]);
          System.out.println(Title[counter] + Author[counter] + Price[counter] + Publisher[counter] + ISBN[counter]);
          counter++;
      }
      else
      {
          System.out.println("Invalid Data format: " + line);
      }
  }

关于java - 无效数据帮助我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47396345/

相关文章:

java - 如何在java中将NULL值插入到数据库表中?

java - 无法解析 com.parse.ParseQueryAdapter

java - 如何编写classname.class能够返回对java.lang.Class对象的引用?

java - 使用 Spring Boot Starter Test 在单元测试中获取异常对象

java - 如果您不使用返回值,Class.forName() 有什么用途?

java - 如何获取通用类中继承方法内的属性值?

java - Morphia 中的复杂查询

java - 加载字体文件时Eclipse Java File FileInputStream vs Input Stream

java - JPA 给自己惹麻烦

java - 是否可以解决不使用更广泛的异常“System.Exception”的问题?