循环标记器和记事本显示

时间:2011-04-07 02:50:13

标签: java file

我们的任务是检索文件并对其进行分类并将其显示在另一个文件中。姓氏名字,然后是成绩。

由于错误

,我遇到循环问题
"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日 程序描述:编写程序 从文件中读取列表 有成绩的学生。还显示 姓氏,名字,然后成绩。 的 * ** * ** * ** * ** * ** * **** /

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

班级 {     public static void main(String [] 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 +“人/人”+                         “这需要额外的信息”+“\ 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

}         catch(例外e)         {
                System.out.println(e.toString());          }         //关闭流          尝试         {             if(std!= null)                std.close();

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

} }

1 个答案:

答案 0 :(得分:0)

每次调用st.nextToken()之前都需要检查st.hasMoreTokens()

就新行而言,Windows需要\ n \ 0

或者,您可以使用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;
        }


}