-1

for my menu, I have an option (case 2) in my menu to display to contents of the file ("Grades.txt"). If I enter exemplar data being:

First Name: Fred
Surname: Smith
Course: IT
Mark: 66

It saves to the file as:

Fred Smith, Computing, 66

Which is correct. However, when I choose option 2 in the menu after, to display the contents of the file, is shows as:

java.io.FileWriter@55f96302

Now I have looked around and can't find anything similar at all.

If anyone could help solve this issue, I would be grateful. Thanks

Code:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class ExamGrades {

    public static void main(String[] args) throws Exception {
        FileWriter grades = new FileWriter("grades.txt",true);
        BufferedWriter bw = new BufferedWriter(grades);
        PrintWriter out = new PrintWriter(bw);
        Scanner scan = new Scanner(System.in);

        int examMark =0;
        String firstName = "";
        String surName = "";
        String course = "";
        String entry = "";
        int menu =0;

        System.out.println("Welcome to the 'GradeEnter' program! ");
        System.out.println("Menu: ");
        System.out.println("1) Enter Student Grade(s)");
        System.out.println("2) View Student Grade(s)");
        System.out.println("3) Delete Grade(s)");
        System.out.println("4) Exit");
        menu = scan.nextInt();
        switch (menu) {
        case 1:
            System.out.print("Please enter student first Name: "); 
            firstName = scan.next(); 

            System.out.print("Please enter student surname: ");
            surName = scan.next();

            System.out.print("Please select student course: "); 
            course = scan.next();

            System.out.print("Please enter student mark: ");
            while (!scan.hasNextInt())
            {
                System.out.print("Please enter a valid mark: ");
                scan.next();
            }
            examMark = scan.nextInt();

            if (examMark < 40)
            {
                System.out.println("Failed");
            }
            else if (examMark >= 40 && examMark <= 49)
            {
                System.out.println("3rd");
            }
            else if (examMark >= 50 && examMark <= 59)
            {
                System.out.println("2/2");
            }
            else if (examMark >= 60 && examMark <= 69)
            {
                System.out.println("2/1");
            }
            else if (examMark >= 70 && examMark <= 100)
            {
                System.out.println("1st");
            }
            else
            {
                System.out.println("Invalid Mark");
            }
            break;

        case 2:
            System.out.println(grades); 
        case 3:
            /*          System.out.print("Please enter a the first name you want to delete: ");
            if (firstName == scan.next()) {
            grades.remove(scan.next());
            }
             */
        case 4:
            System.out.println("Thanks for using 'GradeEnter' ");
            System.exit(0);
        default:
        } 
        entry = (firstName + " " + surName + ", " + course + ", " + examMark);
        out.println(entry);

        out.close();
        scan.close();
    }
}
JaVa
  • 1
  • 1
  • 4
  • You are printing the object itself, not its content. – Laerte Mar 17 '15 at 12:36
  • "when I choose option 2 in the menu after, to display the contents of the file.." why do you think `case 2: System.out.println(grades);` for `FileWriter grades ...` should do and why do you think so? – Pshemo Mar 17 '15 at 12:37
  • Default `toString()` method of an Object is to print the object address: `java.io.FileWriter@55f96302`. If you want to read the file you could look into the `Scanner` class which would allow you to read data from the file. – brso05 Mar 17 '15 at 12:51

3 Answers3

2

grades in FileWriter so you can only write data to file. If you would like to read data from file you need to use for example FileReader http://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html and then print file's content

0

You can't just print the FileWriter object. In particular - you don't need to use FileWriter. Reading a plain text file in Java. This topic can help you.

Community
  • 1
  • 1
Sergey Vasnev
  • 763
  • 4
  • 15
0

I think the program is behaving as expected, you are printing the object itself.

From the question I understand that you wanted to display the content when option 2 is selected. so, if you want to read the contents of the file, you need to use FileReader or BufferedReader.

user85
  • 1,357
  • 4
  • 24
  • 40