writing to File

Posted on 2002-05-12
Medium Priority
Last Modified: 2010-03-31
Hi this is a portion ao the program i am writing i have problem storing the result both the Name of Player and Score in a text file.  It should also update the result if a player with the same name plays again.

Basically it should be like this

Name                Score

Robert              60%
Steve               40%

and so on.

else if ( state == Answer4)

                        if (replyInput.equalsIgnoreCase ("2"))
                                QScore += 20;
                                SendToClient = Name+ "...Congratulations You have scored "+QScore+"%."+" You want to try again? (y/n)";
                                SendToTxtFile = Name+" scored "+QScore+" %";
                                a[0] = new String(SendToTxtFile);
                                state = PlayAgain;

                                SendToClient = Name+ " scored " +QScore+ "%."+"You want to try again? (y/n)"  ;
                                //  {
                                //    FileOutputStream ScoreResults = new FileOutputStream(new File("C:\\score.txt");
                                //        fos.write(SendToClient);
                                //        ScoreResults.close();
                                //    }
                                //    catch(Exception e)
                                //      {
                                //      }

                                SendToTxtFile= Name+ " scored " +QScore+" %";
                                a[0] = new String(SendToTxtFile);
                                state = PlayAgain;



                    else if (state == PlayAgain)

                            if (replyInput.equalsIgnoreCase("y"))
                                SendToClient ="Wellcome To Quiz Game Press P Whenever You Ready";
                                state = MsgWelcome;

                            SendToClient = "Thank you for trying the game have a good day!";

                    return SendToClient;

            public String store()
                    System.out.println("k= "+k);
                    System.out.println("a[0]= "+a[0]);

                    for (int x=0; x<=k; x++)
                        SendToClient= a[x];
                        return SendToTxtFile;


Thank you.
Question by:KfromE
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7005628
so whats the problem ...........????

Author Comment

ID: 7006610
I want to put it into a tect file which is not doing it at the moment.

Accepted Solution

ericpastoor earned 200 total points
ID: 7011522
ON your line where you have FileOutputStream try this instead

you must do an import java.io.*; at the top

BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\score.txt"));
PrintWriter pw = new PrintWriter(bw);

that is how i write text files, and it always works for me.

when you are done you will need to do a
to print something you do pw.println("your text");
or pw.print("Text"); //if you dont want a new line at end...its fairly simple. lemme know if you need help
LVL 35

Expert Comment

ID: 8741266
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to ericpastoor

Please leave any comments here within the
next seven days.


Cleanup Volunteer

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question