我写了一个程序,它读取一个文本文件来显示足球比分,现在文本文件的排列使其包含错误,我正在尝试编写一个程序来计算这些错误。文本文件的排列如下:
Hull City : Sunderland : 2 : 3
Chelsea : Manchester City :1
Fulham : Leeds United : 1 : 2
Wigan : Tottenham : 1 : x
: :2:0
所以上面缺少团队名称,缺少分数,一些分数被X替换。我不能为我的生活弄清楚如何引入计数器来计算错误的数量,任何想法都在一个起点上/解决方案将非常感谢,谢谢!
这是我的完整代码: 主:
public class main {
public static void main(String[] args) {
String userInput;
readFile readScores = new readFile();
do
{
userInput = readScores.getUserInput();
if(userInput.equalsIgnoreCase("S"))
readScores.printScores();
readScores.totalGoals();
readScores.errorCount();
} while (!userInput.equalsIgnoreCase("E"));
System.out.println("****************Exiting application****************");
System.exit(0);
}
}
Readfile类:
public class readFile {
String [] stringArr;
Scanner scan = new Scanner(System.in);
public String getUserInput()
{
String userInput;
System.out.println("Select your option:\nS - Show Scores \nE - Exit");
userInput = scan.nextLine();
return (userInput);
}
public void printScores()
{
String sep = ":";
File inputfile = new File ("P:/SD/Assignment1/results2.txt");
String line = "";
try {
Scanner filescan = new Scanner(inputfile);
while(filescan.hasNext())
{
line = filescan.nextLine();
stringArr = line.split(sep);
if(stringArr.length == 4)
{
System.out.println(stringArr[0]+"\t [" +stringArr[2]+"]\t|" + stringArr[1]+"\t["+ stringArr[3]+" ]\n");
}
else
{
throw new IllegalArgumentException("String " + line + " does not contain " + sep);
}
}
filescan.close();
}
catch (FileNotFoundException e)
{
System.out.println("problem " +e.getMessage());
}
}
public void totalGoals()
{
int[] num = new int[stringArr.length];
int count = 0;
for (int i = 0; i<stringArr.length; i++)
{
System.out.println(stringArr[i]);
num[i] = Integer.parseInt(stringArr[i]);
count = count + num[i];
System.out.println(count);
}
}
public void errorCount()
{
String line;
int errorCount=0;
String[] strArr;
try
{
BufferedReader br = new BufferedReader(new FileReader("P:/SD/Assignment1/results2.txt"));
while(line = br.readLine() != null)
{
strArr = line.split(":");
if(strArr.length==4){
if(strArr[1].trim().isEmpty()) errorCount++;
if(strArr[2].trim().isEmpty()) errorCount++;
if(strArr[3].trim().indexOf("x")>=0) errorCount++;
if(strArr[4].trim().indexOf("x")>=0) errorCount++;
}
}
}
catch(Exception e){
//error handling
}
System.out.println("Error count: "+errorCount);
}
}
UPDATE ::
public void errorCount()
{
String line;
int errorCount=0;
String[] strArr;
String[] parts = line.split(":"); <--- ERROR IS HERE
if (parts.length != 4) {
errorCount++;
}
for (String part : parts) {
if (part.trim().isEmpty()) {
errorCount++;
break;
}
}
if (!(isNumeric(parts[2].trim()) && isNumeric(parts[3].trim()))) { //counts one error, otherwise, check each one of them and if both are not numeric, count this as two errors
errorCount++;
// continue with the following line
}
}
答案 0 :(得分:2)
我会建议这样的事情:
String line;
int errorCount=0;
String[] strArr;
try{
BufferedReader br = new BufferedReader(new FileReader(yourTextFile));
while((line = br.readLine()) != null){
strArr = line.split(":");
if(strArr.length==4){
if(strArr[0].trim().isEmpty()) errorCount++;
if(strArr[1].trim().isEmpty()) errorCount++;
if(strArr[2].trim().indexOf("x")>=0) errorCount++;
if(strArr[3].trim().indexOf("x")>=0) errorCount++;
}
else errorCount++;
}
}
catch(Exception e){
//error handling
}
System.out.println("Error count: "+errorCount);
答案 1 :(得分:1)
您可以根据正则表达式检查线条。每个不匹配的行都包含错误。
正则表达式的起点:
/(.+) : (.+) : (\d+) : (\d+)/
括号允许您获取球队名称和分数。
答案 2 :(得分:0)
int errorCounter = 0; //initialize the errorCounter to zero
try{
BufferedReader br = new BufferedReader(new FileReader(yourTextFile));
while((line = br.readLine()) != null){ //read the file line by line
//Check that each line is split into 4 parts (delimited by ':')
String[] parts = line.split(":");
if (parts.length != 4) {
errorCounter++;
continue; //continue with the following line
}
// Then, check if some of the parts are null, like that:
for (String part : parts) {
if (part.trim().isEmpty()) {
errorCounter++;
}
}
//Finally, you can check if the last two parts contain numbers, using [this `isNumeric()` method][2], like that:
if (!(isNumeric(parts[2].trim())) { //checks if the third part is a number
errorCounter++;
}
if (!(isNumeric(parts[3].trim())) { //checks if the last part is numeric
errorCounter++;
}
} catch(IOException ex) {
System.err.println(ex);
}
可以找到isNumeric()方法here。
请注意,此解决方案会在同一行上计算多个错误。如果你想计算每行一个错误,你可以简单地使用Lorenz Meyer建议的单线程。