Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Saving and Loading

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question Saving and Loading

    Hi im a beginner in Java and was creating a "Who wants to be a Millionaire" program, i wanted to know ways in which i could save and load data, like a game. I have this game set so that after every correct answer it will ask the user if they would like to A. Continue, B. Save and Quit, C. Quit. Id like to learn ways i can do a "save and load" type of thing in my program.
    Thank you!!!!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Saving and Loading

    You could output it to a text file and then read it in (google Java file IO), or you could look into serialization (google Java serialization).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Saving and Loading

    but it is possible right? because i really wasnt sure how it was gonna work..

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Saving and Loading

    It's definitely possible, and the terms I gave you to google are the best place to start
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    nitwit3 (August 2nd, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Saving and Loading

    Excellent ! Thank you VERY much!

  7. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Saving and Loading

    Okay So i was able to research java file IO.... but ... it didnt make sence to me i dont see how it will work for my program. Could someon explain it to me?

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Saving and Loading

    What about it didn't make sense? You would output whatever you wanted to the file, then you'd read it in however you wanted to do the loading. What things are you trying to save and load?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Saving and Loading

    well see as i said in my first post it is a who wants to be a millionaire game, i want it so that the user can play and when wanted they can save the game, come back and continue off from where they started. I've been looking at many examples if file writing but i just dont see how i can put it into my program.

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Saving and Loading

    One of the simplest ways to do it is to just output the stuff you want to save to a file. So you might have something like:

    PlayerName
    Score
    QuestionNumber

    Then to load, you just read the information in the same way.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Saving and Loading

    okay so all i need to do is load to the question the user was on. so how would i go about doing that?

  12. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Saving and Loading

    Quote Originally Posted by nitwit3 View Post
    okay so all i need to do is load to the question the user was on. so how would i go about doing that?
    What do you mean by "load to the question the user was on"?

    Just imagine we are strangers on an internet forum who have no idea how your application is going to work, and explain clearly what you want to achieve.

  13. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Saving and Loading

    okay so what i mean is the user will be playing the game (Who wants to be a millionaire) after every correctly answered question, a menu will pop up to the user with three options
    1. Continue
    2. Save and Quit
    3. Quit
    for the save and quit option i want to make it so that the user can save to where he has gotten to (for example the user finishes the $1000 question and then decides to save and quit, it should save so that when the user returns to the game and hits "Continue Old game" (option in main menu) the user can return back to the $1000 question. how do i do this?

  14. #13
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Saving and Loading

    It's hard to say without knowing how you intend to manage the questions and answers data inside your program, but if you have a particular list of questions and maintain a list of answers for the current player, when the player ends a session, you can save the list of answers (and either the list of questions or some key to the list of questions) in a file or database with that user's name as the file name or database key. When the user wants to resume a game session, you read in the answers for that user (and get the relevant list of questions) into a list, and you can then check the number of answers in the list to find the next question to ask (assuming there is one answer per question!).

    That's just one example. Alternatively, you might keep the questions and answers in pairs in your code, perhaps stored as two fields in a single object. You could then save them to a Properties file for the player, where the question is the property and the answer is the value. Or again, you could use a Map collection to hold the answers keyed by the questions. In either case, when you read the question/answer pairs back from the file, and rebuild the Map, you could find the next question by finding the first question with no corresponding answer.

    Or you could simply save the next question, or the number of the next question, to the player's file along with the other data...

    This kind of design work is the most important part of software development, because the decisions you make about how the program will work before you write any code determine everything else you will do. You should try to think of as many ways of doing it as you can, then try to think of all the advantages and disadvantages these ideas may have. Experience does have a huge influence on how easy this process is, and how good the results are, but you only get experience by doing it - we can give you a few ideas, but you need to think about it yourself, and make the decisions yourself.

    Most importantly, don't write any code until you're sure you know exactly how your program will work - preferably down to pseudo-code level.

  15. The Following User Says Thank You to dlorde For This Useful Post:

    nitwit3 (August 2nd, 2011)

Similar Threads

  1. GUI Not Loading
    By ADAMWD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2011, 01:22 PM
  2. Loading array
    By joshft91 in forum Collections and Generics
    Replies: 3
    Last Post: January 19th, 2011, 11:30 AM
  3. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 12:13 PM
  4. Replies: 3
    Last Post: April 14th, 2009, 08:35 AM