我正在尝试解析我的代码,以便将无效数据编辑为“数据无效”之类的内容。我在使分离器工作时遇到困难。
单词一 - 单词二 - 单词三 - 单词四 - 单词五 有没有更简单的方法来做到这一点,而不是将每个单独的标记排序到数组中。因此,在“-”附加到单词的地方,我想了解如何更改它并显示“无效数据”之类的内容
最佳答案
只需围绕“-”进行拆分,因为这就是分隔数据的方式。每当您在数组中获得一个空格作为索引时,您就知道该数据不包含该数据。请尝试此操作,检查数据是否不是空格或数据是否与数字匹配。
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/