Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...