Error text:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.base / java.util.Scanner.nextLine (Scanner.java:1651) at Main.main (Main.java:58)
Code:
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Scanner; import static java.nio.file.Files.newBufferedWriter; public class Main { private static final String CSV_FILE = "track.csv"; public static void main(String[] args) throws IOException { File file = new File("src/2016_11_02_01.txt"); FileReader fr = new FileReader(file); Scanner sc = new Scanner(fr); /*Блок поиска начала информации о тегах*/ int k = 0;//помогающая переменная String str = sc.nextLine(); while (k == 0) { str = sc.nextLine(); if (str.equals("// Информация по фрагменту")) { k = 1; } } /*Конец блока по поиску информации по фрагменту*/ BufferedWriter writer = newBufferedWriter(Paths.get(CSV_FILE)); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("ID", "Name")); while (str.equals("// Информация по фрагменту")){ /*Считываем информацию по фрагменту*/ String name = sc.nextLine(); name = name.substring(0, name.indexOf('<')); String time_plus = sc.nextLine(); time_plus = time_plus.substring(0, time_plus.indexOf('<')); String length_ms = sc.nextLine(); length_ms = length_ms.substring(0, length_ms.indexOf('<')); String length_uni = sc.nextLine(); length_uni = length_uni.substring(0, length_uni.indexOf('<')); double length_unit = Double.valueOf(length_uni); /*Конец блока по считыванию информации*/ String str_track = sc.nextLine(); String[] str_tr; String delimeter = "\\t"; // Разделитель str_tr = str_track.split(delimeter); // Разделения строки str с помощью метода split csvPrinter.printRecord(str_tr[0], str_tr[1], str_tr[2], str_tr[3], str_tr[4], str_tr[5], str_tr[6], str_tr[7], str_tr[8], str_tr[9], str_tr[10]); while (length_unit != 0) { str_tr = sc.nextLine().split(delimeter); csvPrinter.printRecord(str_tr[0], str_tr[1], str_tr[2], str_tr[3], str_tr[4], str_tr[5], str_tr[6], str_tr[7], str_tr[8], str_tr[9], str_tr[10]); length_unit--; } sc.nextLine(); str = sc.nextLine(); } csvPrinter.flush(); } } The problem is at the end of the while, the next line is not. In the text file it is empty, and then comes the new information, which I need to get. Where is the mistake? Tell me please.
