![](/img/trans.png)
[英]Why is my program reading one less line than there actually is? And why is my array taking in only ones?
[英]Why will my program not display more than one line of data
我嘗試放入多個聯系人,但僅顯示第一個聯系人。 我輸入一個名稱,后跟一個數字。 我要與另一個聯系人說是,然后輸入第二個姓名和電話號碼。 現在,我說不再有任何聯系人,它只會打印第一個聯系人。 我需要能夠打印所有聯系人。
import java.io.FileNotFoundException;
import javax.swing.JOptionPane;
public class PhoneBook {
public static void main(String[] args) throws FileNotFoundException {
String contact;
int moreContact=1;
int choice = JOptionPane.showConfirmDialog(null, "Do you want to add a new contact?");
int i = 0;
java.io.File c = new java.io.File("contacts.txt");
java.io.PrintWriter b = new java.io.PrintWriter(c);
if (choice == 0) {
do {
String name = JOptionPane.showInputDialog("Contact Name: ");
String number = JOptionPane.showInputDialog("Contact Number: ");
contact=(name + " "+ number);
b.println(contact);
b.close();
moreContact = JOptionPane.showConfirmDialog(null,"Do you have another contact?");
} while (moreContact==0);
}
java.io.File h = new java.io.File("contacts.txt");
java.util.Scanner list = new java.util.Scanner(h);
while (list.hasNext()) {
System.out.println(list.nextLine());
}
}
}
在do .. while()
循環中接受一個聯系人后,關閉文件:
b.close();
看到一個聯系人就不足為奇了。 而是在循環外部將其關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.