While adding the lines of a text file to List , i donot want to add empty lines to the List. I tried this but not working :
Scanner s=new Scanner(new File(text.txt));
List <String> names=new ArrayList<String>();
while(s.hasNext()){
if (s.nextLine()!=null){
names.add(s.nextLine());
}}
My text file looks like this:
name1 erer | 26-08-1988
name2 rerer | 13-07-1988
name3 erer | 14-07-1988
name4 weww | 13-07-2001
name5 ewew | 18-10-1987
You skip a line each time (double call to
nextLine()in each iteration), should be:or even:
Code edited to reflect (correct) comments.