[英]How to fix PrintWriter not initializing in If statement
我正在尝试提示用户是否要将数据导出到文件。 如果他们选择 y,那么它应该提示他们希望将其导出到哪个文件。 它打印声明,但从不询问,因此从不工作。
我是 Java 的新手,这是一门在线课程,所以我已经尝试了我目前所知道的一切。 我尝试过在循环之前进行连接、初始化等。
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));
}
我希望它能够将数据导出到指定文件。
您必须使用扫描仪 class 来获取输入。
Scanner sc= new Scanner(System.in);
然后你把输入作为
String userFileC = sc.next();
希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.