Solved

writing to File

Posted on 2002-05-12
5
180 Views
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;
                             }

                        else
                            {
                                SendToClient = Name+ " scored " +QScore+ "%."+"You want to try again? (y/n)"  ;
                                //try
                                //  {
                                //    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;
                            }

                            else
                            {
                            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.
0
Comment
Question by:KfromE
5 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 7005628
so whats the problem ...........????
0
 

Author Comment

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

Accepted Solution

by:
ericpastoor earned 50 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
pw.close();
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
0
 
LVL 35

Expert Comment

by:girionis
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now