How to store text from a file to arraylist and then into Jtextfields?
I would like to store the strings from my file into an ArrayList and then reading the 1st line to textfield1, 2nd line to textfield2, 3rd line line to textfield3.
Basically, i have a search button which searches for the first string and then based on that string it prints the next lines. For instance, if i search for valera1, then i want to read the array and store valera1 in textfield2, valera2 in textfield3, and valera3 in textfield4.
I have tried vector, array, arraylist etc. But nothing seems to work! Please help thanks.
file.txt
valera1
valera2
valera3
luiz1
luiz2
luiz3
So far i have this code below:
Code:
//search by name
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
String stringSearch = textfield1.getText();
BufferedReader bf = new BufferedReader(new FileReader("file.txt"));
String line;
ArrayList<String> myarray = new ArrayList<String>();
while (( line = bf.readLine()) != null){
myarray.add(line);
//textfield2.setText();
//textfield3.setText();
//textfield4.setText();
}
bf.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
});
Re: How to store text from a file to arraylist and then into Jtextfields?
All you are doing in that code is reading the whole file into your array.
That's it.
You are not setting any text fields, or even looking for the search value.