0

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());
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
saum
  • 343
  • 6
  • 16
  • Possible duplicate of [Java reading a file into an ArrayList?](https://stackoverflow.com/questions/5343689/java-reading-a-file-into-an-arraylist) – Samuel Philipp Apr 23 '19 at 18:24

2 Answers2

1

You need to move your print for loop out of your while loop. Each iteration of your while loop is then printing every value in the list. Like so:

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);
        }
        // then print the list
        for(i=0;i<list.size();i++) {
            System.out.println(list.get(i));
        }
    }catch (IOException e){
        // Print error in case of failure.
        System.out.println("Error reading from file " + e.getMessage());
    }
}
Bigbob556677
  • 1,805
  • 1
  • 13
  • 38
Illusion
  • 26
  • 2
0

Just move the for loop outside the while loop.

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());
    }
}
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
eeijlar
  • 1,232
  • 3
  • 20
  • 53