So i am working on a game, and i have a string value, and 3 integer vales, that i would like to save, so when you close the program, and then re open it, if you type in the string value, you will have you integer values.

How do i do this?

Here is the general idea of how you can save some values to a file and read them in later.

public class Settings {
	public String s1;
	public int i1, i2, i3;
}

public void writeSettings(Settings s, String fileName) {
	PrintWriter w = new PrintWriter(fileName);
	w.println(s.s1);
	w.println(s.i1);
	w.println(s.i2);
	w.println(s.i3);
	w.flush();
	w.close();
}

public Settings readSettings(String fileName) {
	Settings s = new Settings();
	BufferedReader r = new BufferedReader(new FileReader(fileName));
	// Read data in the same order as it was written.
	s.s1 = r.readLine();
	s.i1 = Integer.parseInt(r.readLine());
	s.i2 = Integer.parseInt(r.readLine());
	s.i3 = Integer.parseInt(r.readLine());
	r.close();
	return s;
}

The above code was not tested, so fix any bugs you encounter.

Settings type/class is the first 4 lines of the code I posted. They should also be compiled along with the rest of the code. The readSettings and writeSettings methods could belong to a different class. Or you can make part of the Settings class.

To fix the error you are describing you will have to post all the code you are trying to compile.

The code that you sent, i copied and pasted into my own java file, hoping to be able to run it. unfortunatly this did not work.

The code I posted was not supposed to be runnable. It was intended to give you an idea of how to do what you wanted.

Here is the code that you can copy/paste...
Save the following code into a file called Settings.java.

import java.io.*;

public class Settings
{
	public String s1;
	public int i1, i2, i3;

	public void write(String filename) throws Exception
	{
		PrintWriter w = new PrintWriter(filename);
		w.println(s1);
		w.println(i1);
		w.println(i2);
		w.println(i3);
		w.flush();
		w.close();
	}

	public void read(String filename) throws Exception
	{
		// Read data in the same order as it was written.
		BufferedReader r = new BufferedReader(new FileReader(filename));
		s1 = r.readLine();
		i1 = Integer.parseInt(r.readLine());
		i2 = Integer.parseInt(r.readLine());
		i3 = Integer.parseInt(r.readLine());
		r.close();
	}
}

Save the following into a file called Main.java in the same directory as Settings.java

public class Main
{
    public static void main(String[] args) throws Exception
    {
        Settings settings = new Settings();
        settings.s1 = "test string";
        settings.i1 = 1;
        settings.i2 = 2;
        settings.i3 = 3;
        settings.write("settings.txt");
    }
}

Now, compile the source code and run it.

$ javac Main.java
$ java Main

The code that you sent, i copied and pasted into my own java file, hoping to be able to run it. unfortunatly this did not work.

The idea here is that you do your own work. Don't expect to be spoonfed fully working solutions to your homework, rather hope to get hints towards solving your problems.

This article has been dead for over six months. Start a new discussion instead.