简体   繁体   English

如何修复 PrintWriter 未在 If 语句中初始化

[英]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.如果他们选择 y,那么它应该提示他们希望将其导出到哪个文件。 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.我是 Java 的新手,这是一门在线课程,所以我已经尝试了我目前所知道的一切。 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.您必须使用扫描仪 class 来获取输入。

Scanner sc= new Scanner(System.in);

Then you take the input as然后你把输入作为

String userFileC = sc.next();

Hope this might help you.希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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