Search code examples
javacoutcinprintwriterprintstream

File not created by using File.createNewFile();


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.


Solution

  • 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!