0

This is my part of my code. Basically I am trying to store the values from double Avg and char gradeLetter in the arrays doubleAVG and charGRADE. What im getting so far is that on the first loop, lets say Avg = 2 then that will be a stored in the file, but if there is a second loop and the value of Avg changes to 3 then 3 will be saved in the file twice. It deletes 2 and saves the number 3 twice. How can i fix that? I want the first loop to store the first value of Avg that can be 2 and then on the second loop to store the new value of Avg that could be 3 but without overwriting the 2. IGNORE THE COMMENTS.

    try {
        FileWriter fw = new FileWriter(fileN);

        for (int count = 0; count < students; count++) {

            doubleAVG = new double[students];
            charGRADE = new char[students];

            doubleAVG[count] = Avg;
            charGRADE[count] = gradeLetter;

            fw.write("This is the grade of: " + FirstName + " " + LastName
                    + " ");
            fw.write(String.valueOf(doubleAVG[count]) + " ");
            fw.write(charGRADE[count]);
        }
        fw.close();
    } catch (Exception e) {
        System.out.println(e);
    }
3
  • Once you have the array populated, you can simply serialize it into a file. Please refer to stackoverflow.com/questions/1467193/…. Commented Mar 9, 2019 at 13:39
  • Please describe step by step (in points) what would you like to achieve. What you are saving, from, where? Commented Mar 9, 2019 at 13:45
  • IGNORE THE COMMENTS. : Please, don't write in upper case. And why don't you remove these useless comments to make a more readable code? Commented Mar 9, 2019 at 14:02

2 Answers 2

0

Please modify your code to this :

public class Main {

    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter(new File("F://test.txt"));
            Scanner sc = new Scanner(System.in);
            int students = 2;
            double[] doubleAVG = new double[students];
            char[] charGRADE = new char[students];
            double avg = 0.0;
            char gradeLetter;
            String FirstName = "";
            String LastName = "";
            for (int count = 0; count < students; count++) {
                System.out.println("Enter average :");
                avg = sc.nextDouble();
                System.out.println("Enter grade :");
                gradeLetter = sc.next().charAt(0);
                System.out.println("Enter First Name :");
                FirstName = sc.next();
                System.out.println("Enter Last Name :");
                LastName = sc.next();
                doubleAVG[count] = avg;
                charGRADE[count] = gradeLetter;
                fw.write("This is the grade of: " + FirstName + " " + LastName + " ");
                fw.write(String.valueOf(doubleAVG[count]) + " ");
                fw.write(charGRADE[count] + System.lineSeparator());
            }
            fw.close();
            sc.close();
        } catch (Exception e) {
            System.out.println(e);
        }

    }

}

Console Input :

Enter average :
70.45
Enter grade :
B
Enter First Name :
John 
Enter Last Name :
Johnson
Enter average :
90.47
Enter grade :
A
Enter First Name :
Michael 
Enter Last Name :
Jordan

In test.txt :

This is the grade of: John Johnson 70.45 B
This is the grade of: Michael Jordan 90.47 A
Sign up to request clarification or add additional context in comments.

2 Comments

Why did you put sc.close(); at the end? What does that mean?
Scanner class must be closed to remove memory leak.
-1

are you aware of java Serialize method(ology) which creates an xml file of all data structures you have in class file and it's objects, making nested structures in it that look like the objects at particular point of time, just as raw xml? It's possible due to relaying of java on 5 exact native data types, whereas an object is just a collection of these data types. Take a look at the comment above with the link to Stackoverflow answer, I just explained you a bit about the mechanism.

1 Comment

Ill take a look. Thanak you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.