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 5 of 5

Thread: Hopefully a simple question!

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Hopefully a simple question!

    I wrote the following code which (among other things) is supposed to produce a text file to hold data. The file is called "DonorInfo.txt".

    The program creates the file and stores the information from the text boxes. However, all of the data in the data file shows up on the same line instead of as individual entries listed one after another.

    I can't figure out for the life of me how to make the data show up as individual line items in the text file. Please help!

    Here is the code that directly relates to the creation of the file:

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            //Code for the Add button  
     
            if (evt.getSource() == jButton1) {
                FileWriter fwriter = null;
                try {
                    String name = jTextField1.getText();
                    String pledge= jFormattedTextField1.getText();
                    String charity= jTextField3.getText();
                    fwriter = new FileWriter("DonorInfo.txt", true);
                    PrintWriter outputFile = new PrintWriter(fwriter);
                    outputFile.print(name + " ");
                    outputFile.print(pledge + " ");
                    outputFile.print(charity + " ");  
                    outputFile.close();
                } catch (IOException ex) {
                    Logger.getLogger(FundraiserDonor.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        fwriter.close();
                    } catch (IOException ex) {
                        Logger.getLogger(FundraiserDonor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
              }
        }

    Here is the code for the entire program:


    package fundraiser.donor.program;
     
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Rick Sebastian, Melissa Williams, Mychal Marmolijo, James Pryor, Brian Carlson
     */
    public class FundraiserDonor extends javax.swing.JFrame {
         /**
         * Creates new form FundraiserDonor
         */
        public FundraiserDonor() {
            setTitle("Fundraiser Donor Program"); // Display name for frame
            setLocationRelativeTo(null); // Centers frame on the screen
            initComponents();
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jMenu1 = new javax.swing.JMenu();
            jMenuItem1 = new javax.swing.JMenuItem();
            jMenuBar2 = new javax.swing.JMenuBar();
            jMenu4 = new javax.swing.JMenu();
            jMenu5 = new javax.swing.JMenu();
            jMenuBar3 = new javax.swing.JMenuBar();
            jMenu6 = new javax.swing.JMenu();
            jMenu7 = new javax.swing.JMenu();
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jFormattedTextField1 = new javax.swing.JFormattedTextField();
            jTextField3 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            jButton3 = new javax.swing.JButton();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu2 = new javax.swing.JMenu();
            jMenuItem2 = new javax.swing.JMenuItem();
            jMenuItem3 = new javax.swing.JMenuItem();
            jMenuItem4 = new javax.swing.JMenuItem();
            jMenu3 = new javax.swing.JMenu();
            jMenuItem5 = new javax.swing.JMenuItem();
            jMenuItem6 = new javax.swing.JMenuItem();
            jMenuItem7 = new javax.swing.JMenuItem();
            jMenu8 = new javax.swing.JMenu();
            jMenuItem8 = new javax.swing.JMenuItem();
            jMenuItem9 = new javax.swing.JMenuItem();
            jMenuItem10 = new javax.swing.JMenuItem();
     
            jMenu1.setText("jMenu1");
     
            jMenuItem1.setAction(jButton3.getAction());
     
            jMenu4.setText("File");
            jMenuBar2.add(jMenu4);
     
            jMenu5.setText("Edit");
            jMenuBar2.add(jMenu5);
     
            jMenu6.setText("File");
            jMenuBar3.add(jMenu6);
     
            jMenu7.setText("Edit");
            jMenuBar3.add(jMenu7);
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Selections"));
     
            jLabel1.setText("Donor Name");
     
            jLabel2.setText("Donor Pledge");
     
            jLabel3.setText("Charity Name");
     
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
     
            jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getCurrencyInstance())));
            jFormattedTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jFormattedTextField1ActionPerformed(evt);
                }
            });
     
            jTextField3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField3ActionPerformed(evt);
                }
            });
     
            jButton1.setText("Add");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jButton2.setText("Clear");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
     
            jTextArea1.setColumns(20);
            jTextArea1.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
            jTextArea1.setRows(5);
            jTextArea1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
            jScrollPane1.setViewportView(jTextArea1);
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                .addComponent(jFormattedTextField1)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addGap(18, 18, 18)
                            .addComponent(jButton2)))
                    .addGap(0, 51, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1)
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(9, 9, 9)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            jButton3.setText("Exit");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
     
            jMenu2.setText("File");
     
            jMenuItem2.setText("Add");
            jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem2ActionPerformed(evt);
                }
            });
            jMenu2.add(jMenuItem2);
     
            jMenuItem3.setText("Save");
            jMenu2.add(jMenuItem3);
     
            jMenuItem4.setText("Print");
            jMenu2.add(jMenuItem4);
     
            jMenuBar1.add(jMenu2);
     
            jMenu3.setText("Edit");
     
            jMenuItem5.setText("Donor Name(s)");
            jMenu3.add(jMenuItem5);
     
            jMenuItem6.setText("Pledge Amount(s)");
            jMenu3.add(jMenuItem6);
     
            jMenuItem7.setText("Charity Info");
            jMenu3.add(jMenuItem7);
     
            jMenuBar1.add(jMenu3);
     
            jMenu8.setText("View");
     
            jMenuItem8.setText("Donor Name(s)");
            jMenu8.add(jMenuItem8);
     
            jMenuItem9.setText("Pledge Amount(s)");
            jMenu8.add(jMenuItem9);
     
            jMenuItem10.setText("Charity Info");
            jMenu8.add(jMenuItem10);
     
            jMenuBar1.add(jMenu8);
     
            setJMenuBar(jMenuBar1);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(44, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton3))
                    .addGap(44, 44, 44))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton3)
                    .addContainerGap(21, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.exit(0); // This code is assigned to the Exit button 
        }                                        
     
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            //Code for the Add button  
     
            if (evt.getSource() == jButton1) {
                FileWriter fwriter = null;
                try {
                    String name = jTextField1.getText();
                    String pledge= jFormattedTextField1.getText();
                    String charity= jTextField3.getText();
                    fwriter = new FileWriter("DonorInfo.txt", true);
                    PrintWriter outputFile = new PrintWriter(fwriter);
                    outputFile.print(name + " ");
                    outputFile.print(pledge + " ");
                    outputFile.print(charity + " ");  
                    outputFile.close();
                } catch (IOException ex) {
                    Logger.getLogger(FundraiserDonor.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        fwriter.close();
                    } catch (IOException ex) {
                        Logger.getLogger(FundraiserDonor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
              }
        }                                        
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jTextField1.setText(""); // This section is the code for the Clear button
        jFormattedTextField1.setText(""); // It will reset all three input lines when clicked
        jTextField3.setText("");
        }                                        
     
        private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        }                                          
     
        private void jFormattedTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                     
            // TODO add your handling code here:
        }                                                    
     
        private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(FundraiserDonor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(FundraiserDonor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(FundraiserDonor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(FundraiserDonor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new FundraiserDonor().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JFormattedTextField jFormattedTextField1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenu jMenu3;
        private javax.swing.JMenu jMenu4;
        private javax.swing.JMenu jMenu5;
        private javax.swing.JMenu jMenu6;
        private javax.swing.JMenu jMenu7;
        private javax.swing.JMenu jMenu8;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuBar jMenuBar2;
        private javax.swing.JMenuBar jMenuBar3;
        private javax.swing.JMenuItem jMenuItem1;
        private javax.swing.JMenuItem jMenuItem10;
        private javax.swing.JMenuItem jMenuItem2;
        private javax.swing.JMenuItem jMenuItem3;
        private javax.swing.JMenuItem jMenuItem4;
        private javax.swing.JMenuItem jMenuItem5;
        private javax.swing.JMenuItem jMenuItem6;
        private javax.swing.JMenuItem jMenuItem7;
        private javax.swing.JMenuItem jMenuItem8;
        private javax.swing.JMenuItem jMenuItem9;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField3;
        // End of variables declaration                   
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hopefully a simple question!

    how to make the data show up as individual line items
    Add an endline character ("\n") at the end of the line where you want the next String to be on another line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Hopefully a simple question!

    The only place that "\n" would logically fit into the code I have would be in the line:

    outputFile.print(charity + "\n ");

    That field is the last of the three fields I am trying to capture in the file.

    However, the "\n" as depicted above does not fix the problem unfortunately.

    I have another program that does a similar data capture into a file, and it works great! I compared the code side by side and I can't see any differences. So I don't have any idea why one works and the other doesn't.

    Here is the code for the program that works:

    Code for creating the file itself:

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            //Code for the Add button 
     
               if (evt.getSource() == jButton1) {
                FileWriter fwriter = null;
                try {
                    String name = jTextField1.getText();
                    String age= jFormattedTextField1.getText();
                    String email= jTextField3.getText();
                    String phone= jFormattedTextField2.getText();
                    fwriter = new FileWriter("ContactInfo.txt", true);
                    PrintWriter outputFile = new PrintWriter(fwriter);
                    outputFile.print(name + " ");
                    outputFile.print(age + " ");
                    outputFile.print(email + " ");  
                    outputFile.println(phone);
                    outputFile.close();
                } catch (IOException ex) {
                    Logger.getLogger(ContactInformationProgram.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        fwriter.close();
                    } catch (IOException ex) {
                        Logger.getLogger(ContactInformationProgram.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
              }

    Code for the entire program:

    package contactinformationprogram;
    import java.io.*;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Rick Sebastian
     */
    public class ContactInformationProgram extends javax.swing.JFrame {
     
        /**
         * Creates new form ContactInformationProgram
         */
        public ContactInformationProgram() {
            setTitle("Contact Information Program"); // Display name for frame
            setLocationRelativeTo(null); // Centers frame on the screen
            initComponents();
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jTextField3 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jFormattedTextField1 = new javax.swing.JFormattedTextField();
            jFormattedTextField2 = new javax.swing.JFormattedTextField();
            jButton3 = new javax.swing.JButton();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenu2 = new javax.swing.JMenu();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Enter Contact Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
     
            jLabel1.setText("Contact Name:");
     
            jLabel2.setText("Contact Age:");
     
            jLabel3.setText("E-Mail Address:");
     
            jLabel4.setText("Cell Phone Num:");
     
            jButton1.setText("Add");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jButton2.setText("Clear");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
     
            jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
            jFormattedTextField1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
            jFormattedTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jFormattedTextField1ActionPerformed(evt);
                }
            });
     
            try {
                jFormattedTextField2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###-###-####")));
            } catch (java.text.ParseException ex) {
                ex.printStackTrace();
            }
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                    .addGap(26, 26, 26)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jTextField3)
                        .addComponent(jFormattedTextField1)
                        .addComponent(jFormattedTextField2)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                            .addComponent(jButton2)
                            .addGap(28, 28, 28))
                        .addComponent(jTextField1))
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2))
                    .addContainerGap())
            );
     
            jButton3.setText("Exit");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
     
            jMenu1.setText("File");
            jMenuBar1.add(jMenu1);
     
            jMenu2.setText("Edit");
            jMenuBar1.add(jMenu2);
     
            setJMenuBar(jMenuBar1);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(32, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton3)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(32, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                    .addComponent(jButton3)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                        
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            //Code for the Add button 
     
               if (evt.getSource() == jButton1) {
                FileWriter fwriter = null;
                try {
                    String name = jTextField1.getText();
                    String age= jFormattedTextField1.getText();
                    String email= jTextField3.getText();
                    String phone= jFormattedTextField2.getText();
                    fwriter = new FileWriter("ContactInfo.txt", true);
                    PrintWriter outputFile = new PrintWriter(fwriter);
                    outputFile.print(name + " ");
                    outputFile.print(age + " ");
                    outputFile.print(email + " ");  
                    outputFile.println(phone);
                    outputFile.close();
                } catch (IOException ex) {
                    Logger.getLogger(ContactInformationProgram.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        fwriter.close();
                    } catch (IOException ex) {
                        Logger.getLogger(ContactInformationProgram.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
              }
        }                                        
     
        private void jFormattedTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                     
            // TODO add your handling code here:
        }                                                    
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jTextField1.setText(""); // This section is the code for the Clear button
        jFormattedTextField1.setText(""); // It will reset all three input lines when clicked
        jTextField3.setText("");        // This code too
        jFormattedTextField2.setText(""); // This code too
        }                                        
     
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.exit(0); // This code is assigned to the Exit button 
        }                                        
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) throws IOException {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(ContactInformationProgram.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(ContactInformationProgram.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(ContactInformationProgram.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(ContactInformationProgram.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new ContactInformationProgram().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JFormattedTextField jFormattedTextField1;
        private javax.swing.JFormattedTextField jFormattedTextField2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField3;
        // End of variables declaration                   
    }

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

    Default Re: Hopefully a simple question!

    the "\n" as depicted above does not fix the problem
    Where is the code that doesn't work that doesn't add a lineend character where you want it?

    What program are you viewing the file in? Does that program honor a single lineend character as the end of the line? Or does it also require a carriage return: "\r"?

    compared the code side by side and I can't see any differences
       outputFile.print(charity + " ");  
    ...
        outputFile.println(phone);
    Notice the methods being called?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Hopefully a simple question!

    Ah ha!! Got it.... print vs println

    Duh.... Thanks so much!!

Similar Threads

  1. Simple Question
    By Natural Noob in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2012, 06:38 PM
  2. Simple Question (hopefully)
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 2
    Last Post: October 11th, 2011, 09:18 PM
  3. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  4. Help with a simple question
    By allea in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 28th, 2011, 07:46 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM