2011-04-07 23 views
1

我們的任務是檢索文件並對其進行分類並將其顯示在另一個文件中。姓氏名,然後等級。循環標記器和記事本顯示

我有得到一個循環,因爲錯誤

"java.util.NoSuchElementException" 

這要去麻煩,只有當我改變目前存在的,而我環路我有發生。我也有顯示結果的問題。我展示的結果都在一行中,我不能讓它發生。這裏是我的代碼到目前爲止:

import java.util.*; 
import java.util.StringTokenizer; 
import java.io.*; 
import javax.swing.*; 
import java.text.DecimalFormat; 


class Grade 
{ 
    public static void main(String [] args)throws IOException 

    { 
     //declaring  
     String line = ""; 
     StringTokenizer st; 
     String delim = " \t\n\r,-"; 
     String token; 
     String firstname; 
     String lastname; 
     String grade; 
     String S69andbelow="Students with 69 or below\n"; 
     String S70to79 ="Students with 70 to 79\n"; 
     String S80to89= "Students with 80 to 89\n"; 
     String S90to100= "Students with 90 to 100\n"; 
     int gradeint; 
     double gradeavg = 0; 
     int count = 0; 
     File inputFile = new File("input.txt"); 
     File outputFile = new File("output.txt"); 
     FileInputStream finput = new FileInputStream(inputFile); 
     FileOutputStream foutput = new FileOutputStream(outputFile); 

     FileReader reader = new FileReader(inputFile); 
     BufferedReader in = new BufferedReader(reader); 

     Scanner std = new Scanner(new File("input.txt")); 

     Scanner scanner = new Scanner(inputFile); 

     BufferedWriter out = new BufferedWriter(new FileWriter(outputFile)); 

     Scanner scan = new Scanner(S69andbelow);   
     //reading linev 
     line = scanner.nextLine(); 

     st = new StringTokenizer(line, delim); 
     //avoiding selected characters 
     try 
     { 
      while(st.hasMoreTokens()) 
     { 



       firstname = st.nextToken(); //<----error started to happen 
       lastname = st.nextToken(); //here 
       grade = st.nextToken(); //and here 
       //storing tokens into their properties 

       gradeint = Integer.parseInt(grade); 
       //converting token to int 

       gradeavg = gradeavg + gradeint; 
       //calculating avg 

       count++; 
       //recording number of entries 

      if (gradeint <=69) 
      { 
       S69andbelow = S69andbelow + lastname + " " 
         + firstname + " " + "\t" + grade + "\n"; 
      } // saving data by grades  

      else if (gradeint >= 70 && gradeint <= 79) 
      { 
       S70to79 = S70to79 + lastname + " " + firstname 
         + " " + "\t" + grade + "\n"; 
      } // saving data by grades 

      else if (gradeint >= 80 && gradeint <=89) 
      { 
       S80to89 = S80to89 + lastname + " " + firstname 
         + " " + "\t" + grade + "\n"; 
      } // saving data by grades 

      else 
      { 
       S90to100 = S90to100 + lastname + " " + firstname 
         + " " + "\t" + grade + "\n"; 
      } // saving data by grades 


}//end while 

      System.out.println(S69andbelow + "\n" + S70to79 + "\n" 
         + S80to89 + "\n" + S90to100); 
       //caterorizing the grades 

       gradeavg = gradeavg/count; 
       //calculating average 

       DecimalFormat df = new DecimalFormat("#0.00"); 

       out.write("The average grade is: " 
         + df.format(gradeavg)); 


      System.out.println("The average grade is: " 
         + df.format(gradeavg)); 



      Writer output = null;    

      output = new BufferedWriter(new FileWriter(outputFile)); 

      // scanner.nextLine(S69andbelow); 
      //output.write(S69andbelow + "\n" + S70to79 + "\n" 
     //    + S80to89 + "\n" + S90to100); 
     // output.close(); 


} 
     catch(Exception e) 
     { 
       System.out.println(e.toString()); 
     } 
     // Close the stream 
     try 
     { 
      if(std != null) 
       std.close(); 


           } 
     catch(Exception e) 
     { 
      System.out.println(e.toString()); 
     } 

} 
} 

已回答問題。最後的潤色,作爲介紹: /* ** * ** * ** * ** * ** * ** * 程序名稱:等級 姓名:Dennis Liang 交期:2011年3月31日 程序描述:編寫程序 從文件中讀取 學生的列表及其成績。還顯示 姓氏,名字,然後等級。 * ** * ** * ** * ** * ** * ****/

進口java.util中。 ; import java.util.StringTokenizer; import java.io.; import javax.swing。*; import java.text.DecimalFormat;

類 級{ 公共靜態無效的主要(字串[] args)拋出IOException異常

{ 
    //declaring  
    String delim = " \t\n\r,-"; 
    String token; 
    String firstName; 
    String lastName; 
    String grade; 
    String S69andbelow="Students with 69 or below\n"; 
    String S70to79 ="Students with 70 to 79\n"; 
    String S80to89= "Students with 80 to 89\n"; 
    String S90to100= "Students with 90 to 100\n"; 
    String all; 
    int gradeint; 
    double gradeavg = 0; 
    int count = 0; 
    int countTwo = 0; 
    File inputFile = new File("input.txt"); 
    File outputFile = new File("output.txt"); 
    FileInputStream finput = new FileInputStream(inputFile); 
    FileOutputStream foutput = new FileOutputStream(outputFile); 

    FileReader reader = new FileReader(inputFile); 
    BufferedReader in = new BufferedReader(reader); 

    Scanner std = new Scanner(new File("input.txt")); 

    Scanner scanner = new Scanner(inputFile); 

    BufferedWriter out = new BufferedWriter(new FileWriter(outputFile)); 

    Scanner scan = new Scanner(S69andbelow);   

    try 
    { 
    String line, newLine = (String)System.getProperty("line.separator"); 
     //seperating lines 
    while((line = in.readLine()) != null){ 
     //Will only work if the file is properly formatted 
    StringTokenizer st = new StringTokenizer(line, " "); 

    if (st.countTokens() == 3) 
     { 
     firstName = st.nextToken();  
     lastName = st.nextToken(); 
     grade = st.nextToken(); 
     gradeint = Integer.parseInt(grade); 
     //retrieving tokens 
     gradeavg = gradeavg + gradeint; 
     //formula for avg 
     count++; 
     //avg count 

     //organizing by grades 
     if (gradeint <=69) 
    { 
     S69andbelow = S69andbelow + newLine+ lastName + " " 
       + firstName + " " + "\t" + grade + newLine; 
    } 
     else if (gradeint >= 70 && gradeint <= 79) 
    { 
     S70to79 = S70to79 +newLine+ lastName + " " + firstName 
       + " " + "\t" + grade + newLine; 
    } 

     else if (gradeint >= 80 && gradeint <=89) 
    { 
     S80to89 = S80to89 + newLine+ lastName + " " + firstName 
       + " " + "\t" + grade + newLine; 
    } 

     else 
    { 
     S90to100 = S90to100 + newLine+ lastName + " " + firstName 
       + " " + "\t" + grade + newLine; 
    } 

} 否則{ countTwo ++; System.out.println(「有+ countTwo +」person/people「+ 」需要附加信息「+」\ n「); }//通知用戶有多少人 }//缺少信息或太多。

  System.out.println(S69andbelow + "\n" + S70to79 + "\n" 
        + S80to89 + "\n" + S90to100); 
      //caterorizing the grades 

      gradeavg = gradeavg/count; 
      //calculating average 

      DecimalFormat df = new DecimalFormat("#0.00"); 
      //formating 



      System.out.println("The average grade is: " 
        + df.format(gradeavg)); 
      //displaying 


      Writer output = null;    

      output = new BufferedWriter(new FileWriter(outputFile)); 
      //make file 
      output.write(newLine + S69andbelow + newLine + S70to79 + newLine 
       + S80to89 + newLine + S90to100); 
      output.write("The average grade is: " 
        + df.format(gradeavg));  
      //outputing to file  
      output.close(); 
      //saving 

} 趕上(例外五) {
的System.out.println(e.toString()); (標準差=空) std。關( );

      } 
    catch(Exception e) 
    { 
     System.out.println(e.toString()); 
    } 

}}

+0

您可能想添加「家庭作業」標籤。 – louisgab 2011-04-07 03:04:22

+0

您能至少張貼發生異常的行號,可能是堆棧跟蹤的一部分嗎? – R0MANARMY 2011-04-07 03:30:30

+0

我評論了它在編碼中的位置。 – dennis 2011-04-07 03:35:56

回答

0

你需要每次調用st.nextToken()

至於新的生產線,Windows需要\ n \ 0前檢查st.hasMoreTokens()

或者,你可以用它給你爲每個操作系統正確的換行符System.getProperty(「line.separator」)。

String line, newLine = (String)System.getProperty("line.separator"); 
while((line = in.readLine()) != null){ 
//Will only work if the file is properly formatted 
StringTokenizer st = new StringTokenizer(line, " "); 
firstName = st.nextToken(); 
lastName = st.nextToken(); 
grade = st.nextToken(); 
gradeint = Integer.parseInt(grade); 

gradeavg = gradeavg + gradeint; 

count++; 

        if (gradeint <=69) 
     { 
      S69andbelow = S69andbelow + lastname + " " 
        + firstname + " " + "\t" + grade + newLine; 
     } 
     else if (gradeint >= 70 && gradeint <= 79) 
     { 
      S70to79 = S70to79 + lastname + " " + firstname 
        + " " + "\t" + grade + "\n"; 
     } 

     else if (gradeint >= 80 && gradeint <=89) 
     { 
      S80to89 = S80to89 + lastname + " " + firstname 
        + " " + "\t" + grade + newLine; 
     } 

     else 
     { 
      S90to100 = S90to100 + lastname + " " + firstname 
        + " " + "\t" + grade + newLine; 
     } 


} 
+0

那麼,我必須有3個while循環? – dennis 2011-04-07 03:02:41

+0

我不知道你的輸入文件看起來像什麼,但也許你會想單獨分析出每行(嵌套循環也許) – LazyCubicleMonkey 2011-04-07 03:04:58

+0

你能指出來? (成\ n \ 0) 輸入文件是:比爾·克林頓85(輸入) 戈爾100(輸入) 布什95(輸入) Hillery克林頓83(輸入) 麥凱恩72(輸入) 丹納綠87(輸入) 史蒂夫德萊尼76(輸入) 約翰·史密斯(輸入) 貝絲票據60(輸入) – dennis 2011-04-07 03:05:00