2015-10-24 117 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(); 
}