Boa noite rapaziada! Fiz o seguinte código, que está lançando a NoSuchElementException
package br.alura.java.io.test;
import java.io.File;
import java.util.Scanner;
public class TesteLeituraTwo {
public static void main(String[] args) throws Throwable {
Scanner scan = new Scanner(new File("contas.csv"));
boolean tem = scan.hasNextLine();
while (tem) {
System.out.println(scan.nextLine());
}
scan.close();
}
}
console:
CC,2902,258850,João Gabriel,3825
CP,124,737313,Katrine Gomes,2920
CC,1112,1002545,Helena Gomes,5480
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at br.alura.java.io.test.TesteLeituraTwo.main(TesteLeituraTwo.java:12)
porém, ao se utilizar o scan.hasNextLine()
diretamente no while
, roda sem problemas:
package br.alura.java.io.test;
import java.io.File;
import java.util.Scanner;
public class TesteLeituraTwo {
public static void main(String[] args) throws Throwable {
Scanner scan = new Scanner(new File("contas.csv"));
boolean tem = scan.hasNextLine();
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
scan.close();
}
}
console:
CC,2902,258850,João Gabriel,3825
CP,124,737313,Katrine Gomes,2920
CC,1112,1002545,Helena Gomes,5480