Need a space between word using append
I am running the following program, which is supposed to write text entered to a text field to an external file, and then read it back and display it in a text area.... :(nerd): The problem is that although it does work, I am getting several words from the external file read back and displayed mashed together, like if I had the two words bunk, and fried in the file, when it is displayed it would be bunkfried?? :(doh):
This is the program:
Code:
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class Interface implements ActionListener{
JFrame frame = new JFrame("Interface program");
JTextField txt1 = new JTextField(16);
JTextArea txt2 = new JTextArea(24, 24);
JButton b1, b2, b3;
JLabel label1, label2;
public void creata() {
frame.setLayout(new FlowLayout());
b1 = new JButton("b1");
b2 = new JButton("Exit");
b3 = new JButton("b3");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(txt1);
txt1.setEditable(true);
label1 = new JLabel("enter data");
frame.add(label1);
frame.add(txt2);
txt2.setEditable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Week 3 Interface");
frame.setSize(600, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
Interface intr = new Interface();
intr.creata();
}
@Override
public void actionPerformed(ActionEvent ae) {
//throw new UnsupportedOperationException("Not supported yet.");
if(ae.getSource() == b2){
System.exit(0);
}else if (ae.getSource()==b1){
FileWriter writer = null;
try {
String txtread = txt1.getText();
File tempfile = new File("C:/users/zlloyd1/desktop/newtesta.txt");
writer = new FileWriter(tempfile, true);
writer.write("" + txtread);
} catch (IOException ex) {
Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
writer.close();
} catch (IOException ex) {
Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
}
}
}else{
try{File file = new File("C:/users/zlloyd1/desktop/newtesta.txt");
Scanner scan = new Scanner(file);
while(scan.hasNext()){
txt2.append(scan.next());
}
}catch(Exception ex){
String tempa = ex.getMessage();
}
}}
}
I need to find a way to use append, but keep the words separated by a space.:(blush): Please advise!! :(handshake):
Re: Need a space between word using append
Re: Need a space between word using append
Quote:
Originally Posted by
Norm
I am not sure what this is saying?? :(shake):
Re: Need a space between word using append
I was telling anyone reading this thread that this topic is also posted at another site.
Re: Need a space between word using append
And the reason for that is because this question has been answered over there, so anyone answering here would be wasting their time.
It is considered impolite to not inform a forum that you have asked the question elsewhere.
It's OK to ask on multiple forums, you just need to tell them.