Results 1 to 10 of 10
- 07-31-2011, 09:28 AM #1
File I/O: writing variables from another file
I'm trying to create a file that writes to a file the scores of 2 players in a game. In my text, there are many example of writing to a file, however, none of them show how to retrieve/accept a variable (in this case, the score) from another file to use in writing to a new file. Here is my block of code with the suspect lines in pink:
public static void writeToFile()
//the file is appended to the file
FileWriter fwriter = new FileWriter(fileName, true);
PrintWriter outputFile = new PrintWriter(fwriter);//opens the file
MemoryGame gamePlayers = new MemoryGame(name1, name2);
testMatch gameScores = new testMatch(score1, score2);
//the game results are written to file
public static void main(String args)
***************If anyone has a suggestion -- even just a good web site where there are examples of such a thing, I'd really appreciate it.
- 07-31-2011, 12:39 PM #2Member
- Join Date
- Oct 2010
- Rep Power
I think u can try by implementing serialization interface... it will help...
- 07-31-2011, 01:45 PM #3writes to a file the scores of 2 players
Two lines of String data, one line for each player's score?
- 07-31-2011, 09:26 PM #4
Well, for simplicity, I thought 2 columns headed by the player names with each subsequent row listing the players' score (i.e., who won and who lost that particular game) would be a good start. I was also thinking about keeping a running total, but just writing the scores to a file is where I need to start....
- 07-31-2011, 09:27 PM #5
Hello Kowsi: Thank you for the suggestion. I'm not sure what that means, but I will look it up.
- 07-31-2011, 09:30 PM #6
Once you have the format for the file you should be able to build the lines for the file and write them one after the other.
- 07-31-2011, 09:46 PM #7
Ok. I agree with your logic. I just don't think I'm getting the score into the file, however. I have another file where the game actually spits out a winner. That file is called, "testMatch." That's why I wrote the line: testMatch gameScores = new testMatch(score1, score2). In this line, I thought I was creating a new testMatch object called gameScores that had parameters score1 and score2. The code throws an error here, because it can't seem to find score1 and score2, even though I've written this line (and the preceding one) several different ways. I even tried "testMatch.score1" etc, and that didn't work either. I guess my boiled down question is, how to do write these two lines to get the player names and scores when they are each produced in different files? This should be just a method call to these other files, right? Why doesn't it find the variables, then?
- 07-31-2011, 09:49 PM #8Why doesn't it find the variables
Misspelling, out of scope, missing ???
- 07-31-2011, 11:00 PM #9
okay, thank you for your help.. I don't think this can be done because of the way the players and the scores were originally designed...
- 07-31-2011, 11:11 PM #10
- By bigsonny in forum New To JavaReplies: 14Last Post: 07-15-2011, 05:22 AM
- By Swiftnsilent in forum New To JavaReplies: 11Last Post: 05-05-2011, 11:40 PM
- By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 02:29 AM
- By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM
- By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM