[SOLVED]Saving and Loading

I am currently making a 2d game but there are some problems with saving and loading. Here is my saving methgod which works perfectly:

public static void PLAYER_AND_WORLD_DATA_SAVE() {
    FileWriter writer = null;

    for (int a = 0; a < 30; a++) {
        try {
            writer = new FileWriter("saves/PlayerAndWorld.sav");
			writer.write(Integer.toString(Game.world.x));
			writer.write(System.getProperty( "line.separator" ));
			writer.write(Integer.toString(Game.world.y));
			writer.write(System.getProperty( "line.separator" ));
			writer.write(Integer.toString(Game.world.type));
			writer.write(System.getProperty( "line.separator" ));
			writer.write(Integer.toString(Environment.hours));
			writer.write(System.getProperty( "line.separator" ));
			writer.write(Integer.toString(Environment.minutes));
			writer.write(System.getProperty( "line.separator" ));
			writer.write(Player.name);
			writer.write(System.getProperty( "line.separator" ));
			writer.write(Integer.toString(Player.money));
			writer.write(System.getProperty( "line.separator" ));
            
			writer.close();
            writer = null;
            
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }

    if (writer != null) { try { writer.close(); } catch (Exception e) {} }
}

However the following loading code doesnt work and hightlights underlines the words…(int)scanner.nextLine();

	public void PLAYER_AND_WORLD_DATA_LOAD() {
	    Scanner scanner = new Scanner("saves/PlayerAndWorld.sav");
	
		while (scanner.hasNextLine()) {
			Game.world.x = (int)scanner.nextLine();
		}
		scanner.close();
	}

The world.x is infact an integer and im tryting to convert the strings within the text document into an int so i can set the world.x to this number

Use the Integer.parseInt(String) method.

Im sure i tried that yesterday and it didnt work but ive tried it now and it works, thanks :slight_smile:

Assuming Scanner is the class introduced in Java6 you should be using the specific method rather than casting, i.e.


final Scanner sc = new Scanner( ... );
final int i = sc.nextInt();
...

Or you can use


String.valueOf(x)

could you not?

You could, however the valueOf method returns an Integer object that would be unboxed, which would be wasteful. Using the parseInt method is better since it returns a primitive int thus there’s no need to unbox it.

But he’s saving the data, not loading it, so I don’t think it matters which way he chooses. Loading is a different story, and I would only use parseInt, but I was just throwing another option out for him for saving.

Edit:
Oh, you thought I meant when loading the data. No, I was suggesting when saving data.

It worked in eclipse but when the program runs i get this error in the console:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "saves/PlayerAndWorld.sav"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)

It’s trying to parse the actual string “saves/PlayerAndWorld.sav” rather than the contents of the file, you need to do the following:

final Scanner sc = new Scanner( new File( "saves/PlayerAndWorld.sav" ) );

RTFM :wink: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

This worked…Thanks!!! ;D