[英]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.