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?
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
}
You open the file for editing but write nothing, what is to be written and how it is to be written?
– user28595
good thanks article for responding, basically it should write the arrival and output, the writing mode should come through the user !
– w_yll
See the answer below.
– user28595