Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

Advanced search

1211227 Posts in 52541 Topics- by 43348 Members - Latest Member: cyberlizzard

April 30, 2016, 08:15:10 pm

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Help with a Java game
Pages: [1]
Author Topic: Help with a Java game  (Read 584 times)

View Profile
« on: March 19, 2012, 04:03:16 pm »

Hi, I'm new here at Tigsource, I really don't know if this is the place to be asking about this but here is my dilemma.
I am creating a small little game in Java to help me learn more about programming, and by little I mean LITTLE, text based sort of thing, really simple.  It's a game called Sword Game, and my only issue so far is writing to a simple text file.  I have a file called playerCfg.cfg, and I have created a method for reading the values in the file with a scanner.  Basically, everything is in one line, with a word to indicate the type of info that the game needs, like level or gold, and then a number next to it to indicate the value.  So, I use something like
This is the method for setting it (so far)

public static void setPlayerNumbersCFG(int type, int amount)throws IOException
                //the scanner crap is copy pasted from the readPlayerNumbersCFG() to save time writing more code
Scanner cfgPlayerNumScan = new Scanner(new File("playerConfig.cfg"));

if(type == 0)
                        //again, the contents of this if() are copy pasted from the reading method
cfgPlayerNumScan.findInLine("level ");
//there's more code here..........

Now with the method for writing each value I need to find "level" and then change the number next to it to something else.  I have looked all over google and all the tutorials are very confusing, and they all seem to give examples for more complex operations than reading from a simple text file.  If anybody could give me a simple snippet of code explaining what I would have to do that would be awesome.

EDIT: I put in my actual code and changed my wording a little bit to make it more clear.
« Last Edit: March 19, 2012, 09:20:09 pm by newcster2 » Logged
Level 6

View Profile
« Reply #1 on: March 20, 2012, 02:52:59 pm »

When you're doing file operations, the simplest way is to open a file for either reading or writing. Instead of just changing 1 thing, it would probably be easier to write the entire config file each time, especially if you're changing more than one variable (assuming there's stuff like "gold" and "score" in there as well that would need changing at the same time).
Level 10

View Profile
« Reply #2 on: March 22, 2012, 03:42:41 am »

The easiest (and probably best) way is to use one of Java's built in solutions for this problem.

You can for example use ·properties files (instead of .cfg). Then you can read and write things to it using the Properties class. (The Properties class can also handle .cfg files, but that requires a few more lines of code)

A simple example can be found here:

A more thorough tutorial can be found here:

If you decide to use the Properties class, feel free to ask more questions about the specifics Smiley
(Or if you want to do it manually, as a learning experience, I can probably help you with that as well)

« Last Edit: March 22, 2012, 07:42:54 am by Dacke » Logged

programming • veganism • feminism • free software
Pages: [1]
Jump to:  

Theme orange-lt created by panic