I've done all the assignments so far, but I really can't get this to work no matter how much i re-read the chapter about reading from files.
What I'm asked to do is this (translated from swedish):
"Create an application that lets the user write in a name in a JTextField,
after the input, make the program search through a .txt file for the name, then print a result message in a JLabel."
I do understand how to write a swing application with basic GUI and such, and I know how to read from a file.
But the book never told me about finding a specified string from a file.
And sorry, the var's are all in swedish, but I hope you can understand what I've done.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Readlist extends JFrame {
private JLabel instruktion;
private JTextField namnruta;
private JLabel resultat;
private JButton sok;
private String namn;
private String file = "namnlista.txt";
class Lyssnare implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == namnruta) {
String data = namnruta.getText();
namn = data;
} else {
try {
boolean hittad = false;
FileReader fr = new FileReader(file);
BufferedReader bf = new BufferedReader(fr);
String line = bf.readLine();
// Here is where I need help!
}
bf.close();
if (!hittad) {
resultat.setText(namn + " hittades inte.");
}
} catch (IOException ex) {
}
}
}
}
public Readlist() {
super("Sök efter namn");
JPanel p = new JPanel();
p.setBackground(Color.white);
setContentPane(p);
instruktion = new JLabel("Sök efter namn i namnlista.txt");
namnruta = new JTextField(15);
sok = new JButton("Sök");
resultat = new JLabel("");
p.add(instruktion);
p.add(namnruta);
p.add(sok);
p.add(resultat);
Lyssnare minLyssnare = new Lyssnare();
sok.addActionListener(minLyssnare);
}
}
Hopefully I can get some help in this
edit: YES, I've searched, but unfortunately I couldn't understand any of the responses given in other topics
This post has been edited by dansaDisco: 15 May 2011 - 10:53 AM

New Topic/Question
Reply


MultiQuote


|