I am reading a file content to ArrayList so I can later manipulate the data. But when I try to print to console the content is repeatedly displayed. I online want to be able to print the five lines. How can i adjust the code so that when i display on the console it can only display the five lines with repeatedly displaying the results? In the file I have
3456
1678
4354
2384
5634
After reading to list and display to console the result is
3456
3456
1678
3456
1678
4354
3456
1678
4354
2384
3456
1678
4354
2384
5634
I only want to display the five lines.
3456
1678
4354
2384
5634
Code:
public void testread(){
System.out.println("Enter filename:\n");
String filename=Keyboard.readInput();
File myfile=new File(filename);
try (BufferedReader scanfile=new BufferedReader(new FileReader(myfile))) {
String str;
List<String>list=new ArrayList<String>();
while ((str=scanfile.readLine())!=null) {
int i;
list.add(str);
for (i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
}
} catch (IOException e) {
System.out.println("Error reading from file " + e.getMessage());
}
}