Results 1 to 2 of 2

Thread: Saving Data

  1. #1
    Nazrininator is offline Touhou-Stepmania Fan
    Join Date
    May 2014
    Location
    USA
    Posts
    11
    Rep Power
    0

    Default Saving Data

    So I'm still learning how to save data using Java. I know that saving data is extremely important when it comes to creating video games.

    What I have here is this simple program.

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    
    public class Character
    {
       private Formatter x;
       private int roomNum;
       private boolean[] visited = new boolean[10];
       private String myName;
       
       public Character(String name)//Creates a character automatically placed in room 1
       {
          for(int i = 0; i < visited.length; i++)
             visited[i] = false;
          roomNum = 1;
          visited[0] = true;
          myName = name;
       }
       
       public String getName()
       {
          return myName;
       }
       
       public int getRoomNum()//Returns the room number
       {
          return roomNum;
       }
       
       public void goToNextRoom()//Makes the character go to the next room.
       {
          roomNum++;
          if (roomNum > 10)//If the character is at room 10, he will go to room 1.
             roomNum = 1;
          visited[getRoomNum()-1] = true;
       }
       
       public void goToPreviousRoom()//Makes the character go to the previous room.
       {
          roomNum--;
          if (roomNum <1)//If the character is at room 1, he will go to room 10.
             roomNum = 10;
          visited[getRoomNum()-1] = true;
       }
       
       public boolean hasVisited(int num)//Returns if the character has visited the room or not.
       {
          return visited[num-1];
       }
       
       public void saveData()
       {  
          openFile();
          addRecords();
          closeFile();
       }
       
       public void openFile()//Creates a file
       {
          try
          {
             x = new Formatter(getName() + " data.txt");
          }
          catch(Exception e)
          {
             System.out.println("Error Opening Data");
          }
       }
       
       public void addRecords()
       {
          x.format("%s %d", getName(), getRoomNum());
       }
       
       public void closeFile()
       {
          x.close();
       }
    }
    
    //This program puts a character in a circular house with 10 rooms.
    
    public class CircularHouse
    {
    	public static void main( String args[] )
    	{
          Character joe = new Character("Joe");
          joe.goToPreviousRoom();
          for(int i = 1; i <= 4; i++)
             joe.goToNextRoom();
          joe.saveData();
       }
    }
    I'm trying to make sure that after I exit the program, joe starts in room 4, not room 1, and that he has visited rooms 1, 2, 3, 4, and 10. I have the save data created, but how do I save an array of boolean values?
    Last edited by Nazrininator; 09-18-2014 at 06:52 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Saving Data

    You may want to read up on serialization. It's a way to keep objects persistent between invocations of a given application.
    The difficult part would be to maintain control state. Let's say you are in a loop of 10 iterations of a turn and decide after 6 iterations
    to allow the user to save. Upon return to the game, the user needs to be placed in the same spot, or just before the 7th iteration
    is to commence.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. saving data b4 quitin
    By sanzy in forum New To Java
    Replies: 8
    Last Post: 03-25-2011, 04:00 PM
  2. Hi! Saving data??
    By Gahstanz in forum New To Java
    Replies: 1
    Last Post: 06-29-2010, 06:29 AM
  3. What do you use for saving data from table?
    By ocean in forum New To Java
    Replies: 1
    Last Post: 11-21-2009, 07:08 PM
  4. Saving data...?
    By easyRyder in forum New To Java
    Replies: 8
    Last Post: 07-15-2008, 03:14 AM
  5. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 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
  •