0

I am trying to create a .json file with with part of the filename coming from user input. However I keep getting the null value in the resulted filename: it is nullworkoom.json where null should actually be a string coming from the user.

This is part of my code:

public class WorkRoomApp {
    private String name;
    //private static final String JSON_STORE = "./data/workroom.json";
    private String JSON_STORE = "./data/" + name +"workroom.json";

    private Scanner input;
    private WorkRoom workRoom;
    private JsonWriter jsonWriter;
    private JsonReader jsonReader;


  // Step 1
  public WorkRoomApp() throws FileNotFoundException {

        jsonWriter = new JsonWriter(JSON_STORE);
        jsonReader = new JsonReader(JSON_STORE);
        runWorkRoom();  // Step 2
    }

// Step 2
private void runWorkRoom() {
    boolean keepGoing = true;
    String command = null;
    input = new Scanner(System.in);

    new_customer();  // Step 3

 // Step 3
private void new_customer(){
    System.out.println("Are you first time here?");
    System.out.print(("Press 1 for yes, and 0 for no"));
    input = new Scanner(System.in);

    String yes_no;
    yes_no = input.next();
    if (yes_no == "1"){
        create_new_json();    // step 4: because I pretend it is a new user
    } else {
        load_old_json();
    }
}



private void create_new_json(){
    System.out.println("Please enter your name without space");
    String filename = null;
    Scanner input = new Scanner(System.in);
    filename = input.next();
    this.name = filename;   // so I thought the name field in the class 
    // has not been filled with the actual filename string now.
    // so that the field JSON_STORE will not equal 
    // "./data/ABCworkroom.json"
    // if the person enter ABC when asked "Please enter your name without space"
    // but somehow I got the file nullworkroom.json 

could someone tell me where my mistake could be in the code?

thank you

john_w
  • 693
  • 1
  • 6
  • 25
  • Read [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging your code. – Code-Apprentice Aug 16 '21 at 23:40
  • @Code-Apprentice With all due respect, I think the real problem was not a string comparison issue, but rather the author setting the ```JSON_STORE``` variable before the ```name``` variable was set and then forgetting to reset it. – Chaos_Is_Harmony Aug 17 '21 at 02:15
  • @Chaos_Is_Harmon Yes, it looks like that is also a problem. – Code-Apprentice Aug 17 '21 at 14:47
  • 1
    Side note: `ALL_CAPS` variable names are generally used for `static final` variables as in your original version. Now that you changed `JSON_STORE` non-static and non-final, I suggest renaming it `jsonStore` just like your other `private` variables. – Code-Apprentice Aug 18 '21 at 16:49

1 Answers1

1

You need String.equals() in new_customer().

if (yes_no.equals("1"))

AND

You need to reset the JSON_STORE variable in create_new_json() like this:

this.name = filename;
JSON_STORE = "./data/" + name +"workroom.json";
Chaos_Is_Harmony
  • 498
  • 5
  • 17