JoeMaynard
JoeMaynard

Reputation: 33

Make one extra variable, program doesn't work

My task is to create a program which asks the user to enter a year, a first name and a last name. It then takes the last 2 numbers of the year, the whole last name, and the first letter of the first name and formats them into an email like this: [email protected]. It places this email in a text file, which doesn't need to be printed. At the end, it asks if the user wishes to repeat the process again to make a new email.

This is my program, and it isn't fully complete yet. I have it working, but when I go to implement the part which repeats it if wanted, the email is no longer made in the file:

public static void main(String[] args) throws IOException {
    PrintWriter pw = new PrintWriter (new FileWriter("7D_mail.txt"));
    boolean done = false;
    while (done==false){
    Scanner kb = new Scanner (System.in);
    System.out.print ("Enter the year (e.g 2016) > ");
    String year = kb.nextLine();
    System.out.print ("Enter your first name > ");
    String fname = kb.nextLine();
    System.out.print ("Enter your last name > ");
    String lname = kb.nextLine();
    pw.write (year.substring(2)+lname+fname.charAt(0)+"@mymail.co.uk");
    System.out.print ("*** Email created - another one? (Y/N)");

    pw.close();
    }


}

This program above works, but if I then add one line after the last one (String answer = kb.nextLine();), to make a new string for the answer, it no longer works.

public static void main(String[] args) throws IOException {
    PrintWriter pw = new PrintWriter (new FileWriter("7D_mail.txt"));
    boolean done = false;
    while (done==false){
    Scanner kb = new Scanner (System.in);
    System.out.print ("Enter the year (e.g 2016) > ");
    String year = kb.nextLine();
    System.out.print ("Enter your first name > ");
    String fname = kb.nextLine();
    System.out.print ("Enter your last name > ");
    String lname = kb.nextLine();
    pw.write (year.substring(2)+lname+fname.charAt(0)+"@mymail.co.uk");
    System.out.print ("*** Email created - another one? (Y/N)");
    String answer = kb.nextLine();

    pw.close();
    }


}

Any idea why this doesn't work? Thanks

Upvotes: 0

Views: 79

Answers (3)

Laura Paola Gamboa
Laura Paola Gamboa

Reputation: 11

Try using System.out.println (); after printing the question to avoid exceptions (you can read the api here https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()).

Upvotes: -1

sosale151
sosale151

Reputation: 380

This is because the value 'done' is set to true after the first iteration of the loop and the loop never gets a chance to run again.

Besides, you close the PrintWriter after one iteration as well.

What I would suggest is this change:

if(answer.equals("N")){
    done = true;
    pw.close();
}

Upvotes: 1

Steve
Steve

Reputation: 11

I don't think it stopped working. You are unconditionally setting "done" to true after you take the user's repsonse, so it exits. Wrap "done = true" in a condition that checks for the value of "answer" to be "Y".

Upvotes: 1

Related Questions