0

im having a small problem with my properties files with a PrintWriter. This is the code for the main file:

package org.goverment;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Tg {

    public static final void main(String[] args) throws Exception {
    cout("The Goverment - Create your own country!/");
    cout("Press the number associated with your choice then press enter./");
    cout("1. New Game/");
    cout("2. Continue/");
    cout("3. ExitGame/");
    int c = Integer.parseInt(cin());
    if(c == 1) {
        cout("/");
        cout("Country name: ");
        String name = cin();
        cout("\nIs this a soviet country? (y/n): ");
        String soviet = cin();
        boolean svt;
        if(soviet.equalsIgnoreCase("y") || soviet.equalsIgnoreCase("yes"))
            svt = true;
        else if(soviet.equalsIgnoreCase("n") || soviet.equalsIgnoreCase("no"))
            svt = false;
        else
            svt = false;
        Game.setup(Game.cc(), name, svt);
    } else if(c == 2)
        System.exit(0); // Game.c();
    else if(c == 3)
        System.exit(0);
}

private static String cin() throws IOException {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    return br.readLine();
}


public static final void cout(String s) {
    if(s.endsWith("/") || s.equalsIgnoreCase("")) {
            System.out.println(s.substring(0, s.length() - 1));
        } else {
            System.out.print(s);
        }
    }

}

And this is the Game class: http://pastebin.com/bktg6nSc

This is the problem: The file isint created... i keep flushing and closing but nothing happens. i keep looking at the application data but no thegoverment.properties is there.

So what should i do? I really do need help, its for a school project and i must do it by 2 days.

  • I believe I can help debug better if you include the output, if any, has arrived at System.out from cout after this is run I assume that IOException doesn't get thrown at any point. – newcoder Aug 09 '14 at 20:17
  • @spoko its just information -.- – nestorishimo10 Aug 09 '14 at 20:19
  • @newcoder no output, only asks the questions, after that, its stuck. – nestorishimo10 Aug 09 '14 at 20:19
  • Your comment that it's "stuck" after asking the questions is the key piece that told me that the bug was probably somewhere between the questions and the file creation, especially since your use of File is correct. Without you narrowing it down, I wouldn't have had time to read and check both classes. Thank you, and in the future please include in your question where the program stops behaving correctly. :-) – newcoder Aug 10 '14 at 23:07

2 Answers2

0

If adding

writer.flush();

before closing does not work, then, intead of

PrintWriter writer = new PrintWriter(fl);
...

do

BufferedWriter writer = new BufferedWriter(fl);
writer.write("###################");
writer.newLine();
...
writer.flush();
writer.close();

If this still does not work try to create the file where you are executing the program, so:

File fl = new File("thegoverment.properties");
ipinyol
  • 336
  • 2
  • 12
0

The bug is in Game.cc(). The method never returns if the for loop executes as written, so even though your code looks like it calls Game.setup(), the JVM never actually gets to execute it.

The problem is that regardless of the value of done when the while loop finishes, done is always reset to false before the while loop begins again. Classic infinite loop, and completely unrelated to your IO.

I had the following start to Game.cc() when I found the bug. Note the output lines added to aid in debugging.

public static final List<String> cc() {
            List<String> countryNames = new ArrayList<String>();
            System.out.println("About to begin for loop in Game.cc()");
            for (int i = 0; i < 48; i++) {
              System.out.println("for loop iteration "+i);     
              boolean done = false;
                    while (!done) {

You need to declare flag variables such as "boolean done = false;" outside the loop. Write future code like this:

public static final List<String> cc() {
            List<String> countryNames = new ArrayList<String>();
            boolean done = false;
            for (int i = 0; i < 48; i++) {
                    while (!done) {

I should note that thegovernment.properties was correctly created after the fix, though not where one would expect to find it, since you hard-coded to Windows structures and I did not adjust the address before testing on linux. I found thegoverment.properties in the top--level folder of my package.

I should also note that after the properties file is created, it doesn't get modified ever again by Game.setup(), such as when the player starts a new game. Check your logic and test thoroughly to ensure that it's behaving the way you expect.

Best of luck in your studies!

newcoder
  • 334
  • 2
  • 12