Ayuda para terminar archivos java

Vanel

Bovino maduro
#1
Que tal hermanos bakunos!!! Muy buenas noches.

El motivo de este tema es porque pido a ustedes de favor que me ayuden conun código en netbeans, es sobre archivos; mi problema es este: Tengo 4 botonesagregar alumnos, mostrar(imprimir en un textArea), crear archivo y buscar, yacasi tengo todo, el único problema es en el botón buscar; por ejemplo siagregue 4 alumnos llamados Carlos, escribo Carlos y le doy en buscar me debe demostrar los 4 alumnos llamados Carlos y es donde yo no puedo, ya tengo el botónbuscar pero solo me muestra un Carlos y así sucesivamente con los demásalumnos, solo me encuentra un alumno que debo implementarle a mi código?...Miren este es todo mi codigo:
NOTA: El método buscar lo puse en comentario concolormorado ok? disculpen si copie y pegue todo el código, es que no sabía sicopiar solo los métodos o qué onda pf!!!

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* PantallaPadre.java
*
* Created on 3/11/2011, 08:42:17 PM
*/
package mipakete;
import java.io.*;
/**
*
* @author carpego17
*/
public class PantallaPadre extends javax.swing.JFrame {
private File f;
String nombre;
String apellido;
String matricula;
String hom;
/** Creates new form PantallaPadre */
public PantallaPadre() {
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();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
nom = new javax.swing.JTextField();
ape = new javax.swing.JTextField();
mat = new javax.swing.JTextField();
MOSTRAR = new javax.swing.JButton();
AGREGAR = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
pantalla = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
buscar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("LISTA");
jPanel1.setBackground(new java.awt.Color(102, 102, 102));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "ARCHIVOS DE TEXTO", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Vani", 1, 14), new java.awt.Color(0, 255, 0))); // NOI18N
jLabel1.setFont(new java.awt.Font("Trebuchet MS", 2, 12));
jLabel1.setForeground(new java.awt.Color(255, 255, 0));
jLabel1.setText("APELLIDO");
jLabel3.setFont(new java.awt.Font("Trebuchet MS", 2, 12));
jLabel3.setForeground(new java.awt.Color(255, 255, 0));
jLabel3.setText("MATRICULA");
jLabel4.setFont(new java.awt.Font("Trebuchet MS", 2, 12));
jLabel4.setForeground(new java.awt.Color(255, 255, 0));
jLabel4.setText("NOMBRE");
nom.setBackground(new java.awt.Color(102, 102, 102));
nom.setFont(new java.awt.Font("Tahoma", 1, 14));
nom.setForeground(new java.awt.Color(0, 204, 204));
nom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nomActionPerformed(evt);
}
});
ape.setBackground(new java.awt.Color(102, 102, 102));
ape.setFont(new java.awt.Font("Tahoma", 1, 14));
ape.setForeground(new java.awt.Color(0, 204, 204));
mat.setBackground(new java.awt.Color(102, 102, 102));
mat.setFont(new java.awt.Font("Tahoma", 1, 14));
mat.setForeground(new java.awt.Color(0, 204, 204));
MOSTRAR.setBackground(new java.awt.Color(0, 0, 0));
MOSTRAR.setFont(new java.awt.Font("Trebuchet MS", 3, 12));
MOSTRAR.setForeground(new java.awt.Color(255, 255, 0));
MOSTRAR.setText("MOSTRAR");
MOSTRAR.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MOSTRARActionPerformed(evt);
}
});
AGREGAR.setBackground(new java.awt.Color(0, 0, 0));
AGREGAR.setFont(new java.awt.Font("Trebuchet MS", 3, 12));
AGREGAR.setForeground(new java.awt.Color(255, 255, 0));
AGREGAR.setText("AGREGAR");
AGREGAR.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AGREGARActionPerformed(evt);
}
});
pantalla.setBackground(new java.awt.Color(0, 0, 0));
pantalla.setColumns(20);
pantalla.setEditable(false);
pantalla.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 12));
pantalla.setForeground(new java.awt.Color(0, 255, 255));
pantalla.setRows(5);
pantalla.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 0), 3, true));
pantalla.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
pantallaMouseClicked(evt);
}
});
jScrollPane1.setViewportView(pantalla);
jButton1.setBackground(new java.awt.Color(0, 0, 0));
jButton1.setFont(new java.awt.Font("Trebuchet MS", 3, 12));
jButton1.setForeground(new java.awt.Color(255, 255, 0));
jButton1.setText("CREAR ARCHIVO");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setBackground(new java.awt.Color(0, 0, 0));
jButton2.setFont(new java.awt.Font("Trebuchet MS", 3, 12));
jButton2.setForeground(new java.awt.Color(255, 255, 51));
jButton2.setText("SALIR");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "H", "M", " " }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
buscar.setText("BUSCAR");
buscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buscarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(42, 42, 42)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 540, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ape, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(mat, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(43, 43, 43)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(AGREGAR, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(MOSTRAR, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(nom, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(buscar, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(38, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ape, 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, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(mat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(AGREGAR)
.addComponent(buscar))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(MOSTRAR)
.addComponent(jButton2))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jButton1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void nomActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void AGREGARActionPerformed(java.awt.event.ActionEvent evt) {
try{ //METODO QUE AGREGA ALUMNOS...
FileWriter fr=new FileWriter (f,true);
nombre=nom.getText();
apellido=ape.getText();
matricula=mat.getText();
hom=(String) jComboBox1.getSelectedItem();
pantalla.setText(nombre+" "+apellido+" "+matricula+" "+hom);
fr.write(nombre+" "+apellido+" "+matricula+" "+hom+"\n");
fr.close();
}catch(Exception ex){ ex.printStackTrace();
}
nom.setText("");
ape.setText("");
mat.setText("");
}
private void MOSTRARActionPerformed(java.awt.event.ActionEvent evt) {
String var="";//METODO QUE IMPRIME LA LISTA DE TODOS LOS ALUMNOS AGREGADOS...
try{
f=new File ("EsteEsElArchivoCreado.txt");
FileWriter fr=new FileWriter(f,true);
FileReader fr1 = new FileReader(f);
BufferedReader bf = new BufferedReader(fr1);
String linea = null;
while((linea=bf.readLine())!= null){
var+=linea+"\n";
}
pantalla.setText(" LISTA DE ALUMNOS\n\n"+var);
bf.close();
fr.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{//BOTON PARA CREAR EL ARCHIVO...
f=new File ("EsteEsElArchivoCreado.txt");
pantalla.setText(" LISTA DE ALUMNOS\n\n");
}catch(Exception ex){
ex.printStackTrace();
}
}
private void pantallaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);//BOTON PARA SALIR DE LA EJECUCION...
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void buscarActionPerformed(java.awt.event.ActionEvent evt) {
try {//AQUI ESTA EL BOTON BUSCAR!!!... AYUDA PORFA!!!
BufferedReader bufferedReader = new BufferedReader(new FileReader("EsteEsElArchivoCreado.txt"));
String linea1 = "";
String busqueda;
while((linea1 = bufferedReader.readLine())!=null){
busqueda=nom.getText();
if((linea1.indexOf(busqueda)!= -1)){
busqueda+=linea1+"\n";
pantalla.setText(" RESULTADOS...\n\n"+linea1);
}
}
} catch (FileNotFoundException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
}
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("EsteEsElArchivoCreado.txt"));
String linea2 = "";
String busqueda1;
while((linea2 = bufferedReader.readLine())!=null){
busqueda1=ape.getText();
if((linea2.indexOf(busqueda1)!= -1)){
busqueda1+=linea2+"\n";
pantalla.setText(" RESULTADOS...\n\n"+linea2);
}
}
} catch (FileNotFoundException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
}
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("EsteEsElArchivoCreado.txt"));
String linea3 = "";
String busqueda2;
while((linea3 = bufferedReader.readLine())!=null){
busqueda2=mat.getText();
if((linea3.indexOf(busqueda2)!= -1)){
busqueda2+=linea3+"\n";
pantalla.setText(" RESULTADOS...\n\n"+linea3);
}
}
} catch (FileNotFoundException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
}

}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PantallaPadre().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton AGREGAR;
private javax.swing.JButton MOSTRAR;
private javax.swing.JTextField ape;
private javax.swing.JButton buscar;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField mat;
private javax.swing.JTextField nom;
private javax.swing.JTextArea pantalla;
// End of variables declaration
}
 
Arriba