package javaapplication1;

import java.util.*;
import java.io.*;

public class GO {



  public static void main (String[] args)
            throws FileNotFoundException
    {
        Scanner inFile = new Scanner (new FileReader("grades.txt"));
        PrintWriter outFile = new PrintWriter ("output.txt");

        int q1,q2,q3,q4,q5;
        double g = 0;
        int ctr = 0;
        double ave;
        String name;
        double cAve;
        String grade;

        outFile.println("Student\t\tTest1\t Test2\t Test3\t Test4\t Test5\t Average\tGrade");

        while(inFile.hasNext())
        {
            name=inFile.next();
            q1=inFile.nextInt();
            q2=inFile.nextInt();
            q3=inFile.nextInt();
            q4=inFile.nextInt();
            q5=inFile.nextInt();

          ave=calculateAverage(q1,q2,q3,q4,q5);

          grade=calculateGrade(ave);

          ctr++;
          g=g+ave;

          outFile.println(name+ "\t\t" +q1+ "\t " +q2+ "\t " +q3+ "\t " +q4+ "\t " +q5+ "\t " +ave+ "\t\t" +grade+ "\t");
        }

        cAve=g/ctr;
        outFile.printf("\nClass Average = %.2f" , cAve);

        inFile.close();
        outFile.close();
  }


  public static double calculateAverage(int q1,int q2,int q3,int q4,int q5)
    {
        return (double)(q1+q2+q3+q4+q5)/5;
    }

  public static String calculateGrade (double ave)
  {
        if (ave>=90 && ave<=100)

            return ("A");

        else if (ave>=80 && ave<=89)

            return ("B");

        else if (ave>=70 && ave<=79)

            return ("C");

        else if (ave>=60 && ave<=69)

            return ("D");

        else

            return ("F");
  }

}

This is my program but an error occurs

/*Exception in thread "main" java.io.FileNotFoundException: grades.txt (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at java.io.FileReader.<init>(FileReader.java:41)
        at javaapplication1.GO.main(GO.java:15)
Java Result: 1
*/

i need to save grades.txt file on a driver but the problem is, i am using a MAC and i dont know where to save it.. pls help me...

Recommended Answers

All 4 Replies

Scanner inFile = new Scanner (new FileReader("grades.txt"));
PrintWriter outFile = new PrintWriter ("output.txt");

You need to set the exact path of your files. Not just their names, but where they are in the disk.
Where have you saved your files?

Scanner inFile = new Scanner (new FileReader("grades.txt"));
PrintWriter outFile = new PrintWriter ("output.txt");

You need to set the exact path of your files. Not just their names, but where they are in the disk.
Where have you saved your files?

my main program is under Mac-Netbeans-javaapplication-src-go.java

then the grades.txt is under Mac

Maybe because I don't know how MACs work I don't understand you, but:
Where is the file saved?
The full path.
Use that as argument

Try
System.getProperty("user.home")
this should give you the user's home directory, which is a good place to start when you want to write a file somewhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.