Results 1 to 10 of 10
- 07-31-2011, 10:28 AM #1
File I/O: writing variables from another file
Hello:
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()
{
try
{
//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);
displayHistory(gamePlayers);
//the game results are written to file
outputFile.println(gamePlayers.toString());
outputFile.println(theGame.toString());
outputFile.close();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
}
public static void main(String[] args)
{
writeToFile();
readFile();
}
***************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, 01:39 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
I think u can try by implementing serialization interface... it will help...
- 07-31-2011, 02: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, 10:26 PM #4
Hello, Moderator:
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, 10: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, 10: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, 10: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, 10:49 PM #8Why doesn't it find the variables
Misspelling, out of scope, missing ???
- 08-01-2011, 12:00 AM #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...
- 08-01-2011, 12:11 AM #10
Similar Threads
-
Trying to read a file, then writing the info into another file
By bigsonny in forum New To JavaReplies: 14Last Post: 07-15-2011, 06:22 AM -
Problem: Read and Writing Variables to a txt file!
By Swiftnsilent in forum New To JavaReplies: 11Last Post: 05-06-2011, 12:40 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 02:29 AM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 11:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-10-2009, 12:31 AM
Bookmarks