Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

java - BufferReader to write new file with previous info

This is the basis of my code. It prints students grades on the console, but how do I use a Buffereader to put all the students grade on a new file.

import java.io.*;
import java.util.InputMismatchException;
import java.lang.*;
import java.util.*;

public class WorkSpace {

private Scanner x;

    public void openFile(){
        try{
            x = new Scanner (new File ("grades.txt"));
        }
        catch(Exception e){
            System.out.println("could not find file");
        }}



    public void createFile()throws IOException {


        try{
            File file = new File("grades.txt");
            Scanner s = new Scanner(file);




        while(s.hasNext()){
        {
            String [] split = s.nextLine().split(", ");

            String fname = split[0];

            Double q1 = Double.parseDouble (split[1]);
            Double q2 = Double.parseDouble (split[2]);
            Double q3 = Double.parseDouble (split[3]);
            Double q4 = Double.parseDouble (split[4]);
            Double proji = Double.parseDouble (split[5]);
            Double projii = Double.parseDouble (split[6]);
            Double projiii = Double.parseDouble (split[7]);

            double studentgrade = (q1 *0.1) + (q2 *0.1) +(q3 *0.1) + (q4 *0.1) +(proji*0.15) + (projii * 0.2) + (projiii *0.25);
            if(studentgrade>90)
                System.out.printf("%s got an A
", fname);
            else if(studentgrade>80)
                System.out.printf("%s got a B
", fname);
            else if(studentgrade>70)
                System.out.printf("%s got a C
", fname);
            else if(studentgrade>60)
                System.out.printf("%s got a D
", fname);
            else if(studentgrade>50)
                System.out.printf("%s got a F
", fname);


        }}}catch(Exception e){
            e.printStackTrace();
        }



    }
    public void closeFile(){
        x.close();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Scanner.nextInt() returns next integer value read from source (not source length or something). You open the file and try to read integer in the beginning, but the file doesn't start with integer so you get an exception.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...