Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Only getting last word

  1. #1
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Only getting last word

    The following code is supposed to take data entered into a text field, and send it to an external file. Then it is supposed to retrieve that data from the file and read it into a text area on the frame.
    It works, mostly.... Unfortunately, for some reason that I have been unable to see, it will only retrieve the last word from the file??
    For instance, if I enter xxxx1234 into the text field, and click b3, it will put xxxx1234 in the text area, but if I write xxxx 1234 into the text field and click it, I will only get back 1234??
    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.setText(scan.next());
             }
            }catch(Exception ex){
             String tempa = ex.getMessage();
     
             }
            }}
    }
    Can someone tell me why I am only getting the last word, Please??


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Only getting last word

    Check out the JTextArea API and see what the setText(String text) method does. There's another method in that API that will work better for your purposes, that "appends" Strings on to the already existing Strings being displayed.

  3. #3
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: Only getting last word

    So you are saying that the line, txt2.setText(scan.next());, should be txt2.append(scan.next());??
    Or should I add another line of code after txt2.setText(scan.next());??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Only getting last word

    What happened when you tried it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Only getting last word

    I replaced txt2.setText(scan.next());, with txt2.append(scan.next());, and it seemed to work, but it is mashing both words together....
    I guess I need to figure out how to keep a space between them.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Only getting last word

    how to keep a space between them
    A simple way to put a space between two Strings is to concatenate a String with a space:
    string1 + " " + string2

    Also posted at http://www.java-forums.org/new-java/...ng-append.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading a word
    By ashish1765 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 12:44 PM
  2. Replies: 5
    Last Post: August 20th, 2012, 01:01 AM
  3. how to catch the particular word?
    By sxlend in forum Java Theory & Questions
    Replies: 4
    Last Post: July 25th, 2012, 12:30 PM
  4. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  5. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM

Tags for this Thread