2015-10-24 238 views
0

我正在开发一个项目,其中的Java代码必须能够找到考试总成绩,平均分数等等。它从外部的 文件中读取分数。输入验证 - 仅限整数?

我一直在努力寻找一种方法来编辑我的代码,以便它忽略文件中不是0-100之间的整数的任何数据。但我不能。检查了堆栈溢出的所有问题和答案,我找不到有助于我的具体情况的任何答案。下面是我试图处理的代码的while循环:

Scanner reader = new Scanner(file); 

    while (reader.hasNext()) 
    { 
     String line = reader.nextLine(); 
     nextScore = Integer.parseInt(line); 
     System.out.println(nextScore); 
     sum = nextScore + sum; 
     totalNumberOfScores++; 



     if (nextScore > maxScore) 
     { 
      maxScore = nextScore; 
     } 

     else if (nextScore < minScore) 
     { 
      minScore = nextScore; 
     } 

     if (nextScore >= A) 
     { 
      countA++; 
     } 

     else if (nextScore >= B) 
     { 
      countB++; 
     } 

     else if (nextScore >= C) 
     { 
      countC++; 
     } 

     else if (nextScore >= D) 
     { 
      countD++; 
     } 

     else 
     { 
      countF++; 
     } 
    } 

    reader.close(); 

任何人都可以帮忙吗?

+0

显示您正在阅读的文件的格式。 – sam

+0

你面临的问题是什么 –

+0

我面临的问题是我需要我的程序忽略来自外部文件的任何不是整数的数据,但我不能。至于文件的格式,你是什么意思? –

回答

0
if(isInteger(line)){ 
    nextScore = Integer.parse(line); 
} 

public static boolean isInteger(String s) { 
     try { 
      Integer.parseInt(s); 
     } catch(NumberFormatException e) { 
      return false; 
     } catch(NullPointerException e) { 
      return false; 
     } 
     // only got here if we didn't return false 
     return true; 
    } 

,那么你可以做到这一点

boolean isNumber = false; 
for(int i = 0; i < line.length; i++){ 
try{ 
    Integer.parseInt(String.valueOf(line.charAt(i))); 
}catch(Exception e){ 
    isNumber = false; 
    break; 
} 
} 
if(isNumber){ 
Integer.parse(line); 
} 

甚至

boolean isNumber = true; 
try{ 
Integer.praseInt(line); 
}catch(Exception e){ 
isNumber = false; 
} 
if(isNumber){ 
//everthing else 
} 
+0

感谢您的回答!不幸的是,我们还没有涵盖类和方法。我对自己的工作方式很熟悉,但是我的教授可能会这样做。 –

+0

多数民众赞成好吧我添加了一些其他的选择,除非你不能使用try {} catch {},因为这可能后来也是 – Zarch

0

我会通过阅读使用try-with-resources StatementcloseScanner时。接下来,您需要定义您的minmaxtotallines计数在循环外。您可以将min设置为最大可能值,并将max设置为最小值;然后分别使用Math.max(int,int)Math.min(int,int)设置最大值和最小值。然后,您需要验证您是否读取了int并且在将其作为输入进行处理之前处于正确的范围内。类似的,

try (Scanner reader = new Scanner(file)) { 
    int min = Integer.MAX_VALUE; 
    int max = Integer.MIN_VALUE; 
    int total = 0; 
    int lines = 0; 
    int countA = 0, countB = 0, countC = 0, countD = 0, countF = 0; 
    while (reader.hasNextLine()) { 
    String line = reader.nextLine(); 
    try { 
     int nextScore = Integer.parseInt(line); 
     if (nextScore >= 0 && nextScore <= 100) { 
     min = Math.min(nextScore, min); 
     max = Math.max(nextScore, max); 
     total += nextScore; 
     lines++; 
     if (nextScore >= A) { 
      countA++; 
     } else if (nextScore >= B) { 
      countB++; 
     } else if (nextScore >= C) { 
      countC++; 
     } else if (nextScore >= D) { 
      countD++; 
     } else { 
      countF++; 
     } 
     } 
    } catch (NumberFormatException nfe) { 
    } 
    } 
    System.out.printf("Min: %d, Max: %d, Total: %d, Lines: %d, Average: %.2f%n", 
     min, max, total, lines, total/(float) lines); 
    System.out.printf("%d As, %d Bs, %d Cs, %d Ds, %d Fs", countA, countB, 
    countC, countD, countF); 
} catch (IOException e) { 
    e.printStackTrace(); 
}