Results 1 to 10 of 10
  1. #1
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default 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.

  2. #2
    Kowsi is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    I think u can try by implementing serialization interface... it will help...

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    writes to a file the scores of 2 players
    What format do you want the data in the file to be in?
    Two lines of String data, one line for each player's score?

  4. #4
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default

    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....

  5. #5
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default

    Hello Kowsi: Thank you for the suggestion. I'm not sure what that means, but I will look it up.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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.

  7. #7
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default

    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?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Why doesn't it find the variables
    There are many reasons. Without seeing the code I can't say.
    Misspelling, out of scope, missing ???

  9. #9
    pondwire's Avatar
    pondwire is offline Member
    Join Date
    Jul 2011
    Location
    Somewhere in the Rockies
    Posts
    11
    Rep Power
    0

    Default

    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...

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

Similar Threads

  1. Replies: 14
    Last Post: 07-15-2011, 05:22 AM
  2. Problem: Read and Writing Variables to a txt file!
    By Swiftnsilent in forum New To Java
    Replies: 11
    Last Post: 05-05-2011, 11:40 PM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  5. Replies: 3
    Last Post: 05-09-2009, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •