Originally Posted by
PhHein
Add the contracts to a StringBuilder in the loop and display the JOptionPane after the loop.
OK. I heed your advice and added the StringBuilder but I still get the txt in 2 goes; not appearing once in the JOption.
try{
FileReader fileReader = new FileReader("data_file/Contact.txt");
BufferedReader in = new BufferedReader(fileReader);
String currentContact = in.readLine();
StringBuilder sb = new StringBuilder();
while(currentContact != null) {
sb.append(currentContact);
JOptionPane.showMessageDialog(null, "Contact: \n" + currentContact);
// System.out.println("Contact:" + currentContact);
currentContact = in.readLine();
}}
catch (IOException e) {
e.printStackTrace();
}}}
Did I miss out anything?
--- Update ---
Hi,
I modified the code a little and this time round is better - the information will all come out at once. The bad news is that it only appeared on the 2nd time. The first JOptionPane Message still give me the same old one line and not all.
Here's my code and I hope someone can tell me where I have gone wrong:
try{
FileReader fileReader = new FileReader("data_file/Contact.txt");
BufferedReader in = new BufferedReader(fileReader);
String currentContact = in.readLine();
StringBuilder sb = new StringBuilder();
while(currentContact != null) {
StringBuilder current = sb.append(currentContact);
current.append(System.getProperty("line.separator"));
JOptionPane.showMessageDialog(null, "Contact : \n" + current) ;
// System.out.println("Contact:" + currentContact);
currentContact = in.readLine();
}}
catch (IOException e) {
e.printStackTrace();
}}}