[英]Write to text file using ArrayList
該程序基本上是關於從文本文件中讀取,將當前數據存儲到ArrayList中,然后將數據(來自用戶輸入)寫入同一文本文件中。 請讓我知道本小節的錯誤之處? 文本文件中的數據如下:
abc t1 1900
xyz t2 1700
編譯器在輸出行顯示錯誤output.format("%s%s%s%n",
public class justTesting {
private Scanner input;
private Formatter output;
private ArrayList<Student> tk = new ArrayList<Student>();
public static void main(String[] args) {
justTesting app = new justTesting();
app.create();
app.writeToFile();
}
public void create() {
Text entry = new Text();
Scanner input = new Scanner(System.in);
System.out.printf("%s\n", "Please enter your name, ID, and year: ");
while (input.hasNext()) {
try {
entry.setName(input.next());
entry.setTelNumber(input.next());
entry.setDOB(input.next());
for (int i = 0; i < tk.size(); i++) {
output.format("%s%s%s%n", tk.get(i).getName(), tk.get(i)
.getTelNumber(), tk.get(i).getDOB());
}
} catch (FormatterClosedException fce) {
System.err.println("Error writing to file.");
return;
} catch (NoSuchElementException nsee) {
System.err.println("Invalid input. Try again: ");
input.nextLine();
}
System.out.printf("%s\n", "Please enter your name, ID, and year: ");
}
}
public void writeToFile() {
try {
output = new Formatter("testing.txt");
} catch (SecurityException se) {
System.err
.println("You do not have write access permission to this file.");
System.exit(1);
} catch (FileNotFoundException fnfe) {
System.err.println("Error opening or creating file.");
System.exit(1);
}
}
}
我刪除了%n,它起作用了。 感謝大家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.