请帮助,当循环和标记器和读取文件



显然我需要帮助。我们的任务是检索一个文件,对它进行分类,并在另一个文件中显示它。姓,名,然后是年级。由于错误"java.util",我在获得循环时遇到麻烦。NoSuchElementException"这只发生在我改变当前存在的,而我的循环。我也有显示结果的问题。我显示的结果都在一行中,我不能让这种情况发生。我们不允许使用数组列表,只允许使用Bufferedreader, scanner和我已经拥有的。下面是我到目前为止的代码:

import java.util.; 
import java.util.StringTokenizer; 
import java.io.; 
import javax.swing.*; 
import java.text.DecimalFormat;
/*************************************
Program Name: Grade
Name: Dennis Liang
Due Date: 3/31/11
Program Description: Write a program 
which reads from a file a list of 
students with their Grade. Also display 
last name, first name, then grade. 
************************************/
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 = " tnr,-";
        String token;
        String firstname;   
        String lastname;
        String grade;
        String S69andbelow="Students with 69 or belown";
        String S70to79 ="Students with 70 to 79n";
        String S80to89= "Students with 80 to 89n";
        String S90to100= "Students with 90 to 100n";
        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();
                lastname = st.nextToken();
                grade = st.nextToken();
                //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());
         }
    }
}

我的输入文件是这样的:

Bill Clinton 85 (enter)
Al Gore 100 (enter)
George Bush 95 (enter)
Hillery Clinton 83(enter)
John McCain  72(enter)
Danna Green 87(enter)
Steve Delaney 76(enter)
John Smith(enter)
Beth Bills 60(enter)

如果我没有从头到尾跟你讲完的话,你可以帮我指出来。

找到问题的简单方法是注释掉大部分代码并一次找出每一步。所以从能够读取文件开始。然后打印到屏幕上。然后将整理好的数据打印到屏幕上。最后将整理好的数据打印到文件中。

这应该是一个相当简单的

相关内容

  • 没有找到相关文章

最新更新