数据帮助无效 [英] Invalid data helpme

查看:144
本文介绍了数据帮助无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析代码,因此我可以将无效数据编辑为数据无效。我无法让拆分器解决问题。

I'm trying to parse through my code so I can edit the invalid data to something like "Data is invalid." I'm having trouble getting the splitter to work out.

单词一 - 单词二 - 单词三 - 单词四 - 单词五
有没有更简单的方法来执行此操作而不是将每个单独的标记排序到数组中。所以,如果 - 附加到一个单词,我想知道如何更改它并显示类似无效数据的内容

Word One - Word Two - Word Three - Word Four - Word Five Is there an easier way to do this rather than going sorting each individual token into arrays. So where the "-" is attached to a word, I'd like advice on how to change this and display something like "Invalid Data"

推荐答案

只需拆分 - ,因为这就是数据的分离方式。每当您在数组中获得一个空间作为索引时,您就会知道数据不包含该数据。尝试这样做,检查数据是不是空格或数据是否与数字匹配。

Just split around the "-" since that is how the data is being separated. Whenever you get a space as an index in your array then you know the data doesn't contain that data. Try this where you check that the data is not a whitespace or the data matches a digit.

  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);
      }
  }

这篇关于数据帮助无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆