How can I read only one thing in from a textfile?

后端 未结 1 1619
名媛妹妹
名媛妹妹 2021-01-28 20:12

I can read in from the file and am able to change the amount of lines given by changing the number in the for loop but I don\'t want all the numbers in my file displayed side by

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 20:53

    I think what you want to print is

    String randomString = lines.get(r.nextInt(2));
    System.out.println(randomString);
    

    To display only the first 20 random lines from this list of maybe 100

    for (int i = 0; i < 20; i++) {
    
       int rowNum = r.nextInt(lines.size ());
       System.out.println(lines.get(rowNum);
    }
    

    0 讨论(0)
提交回复
热议问题