Captures what the user types and writes to a .TXT

1

I made a program in JFrame, basically it captures what the user types and writes to a txt, but when I go to see the contents of the file is written, the following "null". What could be happening?

importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.util.*;importjava.util.logging.Level;importjava.util.logging.Logger;publicclasstestedabeta07extendsjavax.swing.JFrame{Scannerscan=newScanner(System.in);Scannern;publictestedabeta07(){initComponents();}@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jPanel1 = new javax.swing.JPanel();
        panel1 = new java.awt.Panel();
        jFrame1 = new javax.swing.JFrame();
        Jcampo2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        Bconfirmar = new javax.swing.JButton();
        Bsair = new javax.swing.JButton();
        Jcampo1 = new javax.swing.JTextField();

        jInternalFrame1.setVisible(true);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(5, 5, 10, 10));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(new java.awt.Dimension(360, 237));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Jcampo2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Jcampo2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Jcampo2ActionPerformed(evt);
            }
        });
        getContentPane().add(Jcampo2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 120, 240, -1));

        jLabel1.setText("Chegada");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        jLabel2.setText("Saída");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, -1, -1));

        jLabel3.setFont(new java.awt.Font("Trebuchet MS", 0, 24)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("HALKINGS");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, -1, -1));

        Bconfirmar.setText("Confirmar");
        Bconfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BconfirmarActionPerformed(evt);
            }
        });
        getContentPane().add(Bconfirmar, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 160, -1, -1));

        Bsair.setText("Sair");
        Bsair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BsairActionPerformed(evt);
            }
        });
        getContentPane().add(Bsair, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 160, -1, -1));

        Jcampo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Jcampo1ActionPerformed(evt);
            }
        });
        getContentPane().add(Jcampo1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 70, 240, -1));

        setSize(new java.awt.Dimension(359, 249));
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void BconfirmarActionPerformed(java.awt.event.ActionEvent evt) {                                           
                     File Ponto = new File ("C:\Users\W\Desktop\Ponto\funcionarios.txt");


  try{

  FileWriter fw = new FileWriter(Ponto, true);
  BufferedWriter bw = new BufferedWriter (fw);

  bw.write("" +n);
  bw.newLine();

  bw.close();
  fw.close();

  }catch(IOException ex){
  }

    }                                          

    private void BsairActionPerformed(java.awt.event.ActionEvent evt) {                                      
        System.exit(0);
    }                                     

    private void Jcampo1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

    }                                       

    private void Jcampo2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new testedabeta07().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Bconfirmar;
    private javax.swing.JButton Bsair;
    private javax.swing.JTextField Jcampo1;
    private javax.swing.JTextField Jcampo2;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private java.awt.Panel panel1;
    // End of variables declaration                   
}
    
asked by anonymous 03.04.2018 / 20:33

1 answer

2

You are working with graphical interface, and you do not use the Scanner class to capture data written by the user in this case. If the intent is to capture what is written in the text fields, use getText() of the respective fields:

private void BconfirmarActionPerformed(java.awt.event.ActionEvent evt) {
    File Ponto = new File("C:\Temp\funcionarios.txt");

    try {

        FileWriter fw = new FileWriter(Ponto, true);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(Jcampo1.getText() + " - " + Jcampo2.getText());
        bw.newLine();

        bw.close();
        fw.close();

    } catch (IOException ex) {
    }

}

Another thing, if you will not handle the exception, do not add catch empty, this is a tremendous waste of the feature and still makes it difficult to spot problems. Leave a post here right here on the site with great answers on exception handling.

Recommended sources for study:

03.04.2018 / 20:42