Captures what the user types and writes to a . TXT

Asked

Viewed 143 times

1

I made a little program in Jframe, basically it captures what the user types and writes to a txt, but when I see the contents of the file is written, the following "null". What might be going on?

inserir a descrição da imagem aqui

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class testedabeta07 extends javax.swing.JFrame {
        Scanner scan = new Scanner (System.in);

   Scanner n;

           public testedabeta07() {

        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                   
}
  • 1

    You open the file for editing but write nothing, what is to be written and how it is to be written?

  • good thanks article for responding, basically it should write the arrival and output, the writing mode should come through the user !

  • See the answer below.

1 answer

1


You are working with a graphical interface, and you do not use the class Scanner to capture data written by the user in this case. If the intention is to capture what is written in the text fields, use getText() of their 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 won’t treat the exception, don’t add catch empty, this is a tremendous waste of resource and still makes it difficult to spot problems. Leave below a post right here on the site with great responses on exceptions treatment.

Recommended sources for study:

  • 1

    Poxa life, thank you very much even guy I’m a little trying here and I’m not getting (now it was), thank you very much ! I’ll watch the contents ! vlww

  • @R2D2 remembering that if the answer solved the problem Voce can mark it with accept by clicking on v next to it, this way it is referred as valid solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.