Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED]Saving and Loading  (Read 371 times)
0 Members and 1 Guest are viewing this topic.
Offline Genius Coder

Junior Devvie


Medals: 1
Exp: 4 years



« Posted 2014-01-04 13:37:38 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
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();

1  
2  
3  
4  
5  
6  
7  
8  
   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

I love making games...
Offline quew8

JGO Coder


Medals: 31



« Reply #1 - Posted 2014-01-04 13:39:08 »

Use the Integer.parseInt(String) method.
Offline Genius Coder

Junior Devvie


Medals: 1
Exp: 4 years



« Reply #2 - Posted 2014-01-04 13:43:33 »

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

I love making games...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StrideColossus
« Reply #3 - Posted 2014-01-04 13:46:24 »

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

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

Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2014-01-04 13:49:35 »

Or you can use
1  
String.valueOf(x)

could you not?

Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #5 - Posted 2014-01-04 15:09:12 »

Or you can use
1  
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.

Why are all OpenGL tutorials written in Brainf**k?
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2014-01-04 15:12:13 »

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.

Offline Genius Coder

Junior Devvie


Medals: 1
Exp: 4 years



« Reply #7 - Posted 2014-01-04 15:52:29 »

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


1  
2  
3  
4  
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)



I love making games...
Offline StrideColossus
« Reply #8 - Posted 2014-01-04 15:55:49 »

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


1  
2  
3  
4  
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:

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


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

Offline Genius Coder

Junior Devvie


Medals: 1
Exp: 4 years



« Reply #9 - Posted 2014-01-04 16:10:28 »

This worked...Thanks!!!!  Grin

I love making games...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (79 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!