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());
    }
}
1

2 Answers 2

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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.