我試圖編寫的代碼有一個jTextField供用戶輸入一個字符串。用戶然後將點擊jButton創建文件。爲了創建文件,我想到了:從用戶處取得字符串輸入,並將其附加到文件創建方法中的.txt。下面的代碼顯示了這次不成功的嘗試:如何讓用戶創建一個新的.txt文件,他們選擇在Java
String input;
Scanner scan = new Scanner(System.in);
input = scan; //This is totally wrong, but I'm not sure how to convert the scanner
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
final Formatter x;
try{
x = new Formatter(input + ".txt");
} catch(Exception e) {
System.out.println("Your code is bad. You should feel bad.");
}
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
input = jTextField1.getText();
}
「不成功」:如何? – 2014-02-10 07:18:24
結果是一個null.txt,用戶的string.txt文件是副作用。 – kcmallard
閱讀http://docs.oracle.com/javase/tutorial/essential/io/scanning.html – 2014-02-10 07:21:56