开发者

Help please, while loop and tokenizer and reading files

开发者 https://www.devze.com 2023-02-22 10:05 出处:网络
I need help, obviously. Our assignment is to retrieve a file and categorize it and display it in another file. Last name first name then grade. I am having trouble with getting a loop going because of

I need help, obviously. Our assignment is to retrieve a file and categorize it and display it in another file. Last name first name then grade. I am having trouble with getting a loop going because of the error "java.util.NoSuchElementException" This only happens when I change the currently existing while I loop I have. I also have a problem of displaying the result. The result I display is all in one line, which I can't let happen. We are not allowed to use arraylist, just Bufferedreader, scanner, and what i already have. Here is my code so far:

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 = " \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();
                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());
         }

    }
}

my input file looks like this:

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)

It would help to point things out just in case I don't follow you all the way through.


An easy way of finding a problem in this would be to comment out most of the code and find out each step at a time. So start with being able to read the file. Then print to the screen. Then print the organized data to the screen. Finally print the organized data to the file.

This should be a fairly simple

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号