简体   繁体   中英

How to fix PrintWriter not initializing in If statement

I am trying to prompt the user whether or not they would like to export data to a file. If they pick y, then it should then prompt for which file they would like it to be exported to. It prints the statement, but never asks and therefore never works.

I am new to Java, this is an online course so I have tried everything to my current knowledge. I have tried concatenating, initializing before the loops, etc.

System.out.println("Would you like to export the passwords to a file? (y or n)"); String userFileC = in.next();

    while(!(userFileC.equals("y")) && !(userFileC.equals("n")))
    {
        System.out.println("Would you like to export the passwords to a file? (y or n)");
        userFileC = in.next();
    }
    if(userFileC.equals("y"))
    {
        System.out.println("What is the name of the file? (Remember .txt)");
        String chosenFile = in.next();
        PrintWriter outfile = new PrintWriter(new File(chosenFile));
    }

I would like it to the export data to stated file.

You must use Scanner class to take the input.

Scanner sc= new Scanner(System.in);

Then you take the input as

String userFileC = sc.next();

Hope this might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM