• Post Reply Bookmark Topic Watch Topic
  • New Topic

cant find my file  RSS feed

 
Rayna Boyd
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class StudentDriver {
    public static void main(String[] args) {
        final int NUM140GRADES = 4;
        final int NUM145GRADES = 3;
       
        Scanner userInput = new Scanner(System.in);
        System.out.print("Enter file with student data: ");
        String fileName = userInput.next();
       
        //read the file
        try {
            PrintWriter cs140pw = new PrintWriter(new File("CS140Report.txt"));
            cs140pw.println("Class Report - CS140\n***************************************************************");
            PrintWriter cs145pw = new PrintWriter(new File("CS145Report.txt"));
            cs140pw.println("Class Report - CS145\n***************************************************************");
           
            Scanner fileInput = new Scanner(fileName);
            Student s;
           
            //for each line of the file
            while (fileInput.hasNextLine()) {
                String name = fileInput.next();
                String className = fileInput.next();
               
                s = new Student(name, className);
               
                if (className.equals("CS140")) {
                    s.readGradesSetAverage(fileInput, NUM140GRADES);
                    s.printGradeReport(cs140pw);
                } else if (className.equals("CS145")) {
                    s.readGradesSetAverage(fileInput, NUM145GRADES);
                    s.printGradeReport(cs145pw);
                } else {
                    System.out.println("invalid class: " + className);
                }
            }
           
           
            //remember to close PrintWriter objects
            cs140pw.close();
            cs145pw.close();
        } catch (FileNotFoundException ex) {
            System.out.println("File not found");
        }
    }  
}
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the file in the same directory as your class file?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you expecting the Scanner object's next methods  to return?  Look at the API doc for the Scanner class's constructors to see which one will read from a file and which one will read from the String passed to the constructor.
 
Rayna Boyd
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone skype me to see my errors better, I really need help.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rayna Boyd wrote:can someone skype me to see my errors better, I really need help.

That's not how the forums work. You got two replies within an hour of your question. Reply with the requested info in this thread and we can continue helping.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!