Results 1 to 5 of 5
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question 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.... 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??
    This is the program:
    Java 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. Please advise!!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Need a space between word using append

    Also posted at Only getting last word
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question Re: Need a space between word using append

    Quote Originally Posted by Norm View Post
    Also posted at Only getting last word
    I am not sure what this is saying??

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Need a space between word using append

    I was telling anyone reading this thread that this topic is also posted at another site.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to define what a word is for a word count?
    By Fluxtime in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 10:59 AM
  2. Word Count That Ignores Punctuation And Space
    By stinson in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 04:20 AM
  3. Space Warrior - Multiplayer Space Shooter
    By stes in forum Reviews / Advertising
    Replies: 2
    Last Post: 01-11-2012, 08:19 AM
  4. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  5. Replies: 0
    Last Post: 07-05-2009, 12:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •