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.

  • You can sample text file - Serhii Dikobrazko
  • Honestly, there are a lot of strange points in the code. It makes sense to provide your original txt file. - Mikita Berazouski
  • @Mikita Link to text file: cloud.mail.ru/public/GcTL/acEBN3Fdc And so, I will be glad to hear honest comments. - Vqq50
  • oh ... something frightening. And how does the original task sound? what do you need to do? - Mikita Berazouski
  • @MikitaBerazouski forgot to write the most important thing)) To write a program that creates a csv-file in which all the fragments will be glued into one table - Vqq50

1 answer 1

Based on your answers, this is what will give the result ... even if all of a sudden it is not what you need, the code can be used as a blank.

 import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.nio.file.Paths; import java.util.Scanner; import static java.nio.file.Files.newBufferedWriter; public class Test { public static final String CSV_FILE = "track.csv"; public static final String TXT_FILE = "src/2016_11_02_01.txt"; public static final String STARTS_OF_FRAGMENT = "Время"; public static final String DELIMETER = "\\t"; public static void main(String[] args) throws IOException { File file = new File(TXT_FILE); FileReader fr = new FileReader(file); Scanner sc = new Scanner(fr); CSVPrinter csvPrinter = null; while (sc.hasNext()) { String nextLine = sc.nextLine(); if (nextLine.startsWith(STARTS_OF_FRAGMENT)) { BufferedWriter writer = newBufferedWriter(Paths.get(CSV_FILE)); csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(nextLine.split(DELIMETER))); } else if (csvPrinter != null) { csvPrinter.printRecord(nextLine.split("\\t")); } } csvPrinter.flush(); } } 

As a result, we get: enter image description here

I hope I understood the task correctly.

UPD after discussion in the comments:

 import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.math.BigDecimal; import java.nio.file.Paths; import java.util.Scanner; import static java.nio.file.Files.newBufferedWriter; public class Test { public static final String CSV_FILE = "track.csv"; public static final String TXT_FILE = "src/2016_11_02_01.txt"; public static final String STARTS_OF_FRAGMENT = "Время"; public static final String DELIMETER = "\\t"; public static final String TIME_OF_START = "Время начала фрагмента от начала исследования"; public static void main(String[] args) throws IOException { File file = new File(TXT_FILE); FileReader fr = new FileReader(file); Scanner sc = new Scanner(fr); CSVPrinter csvPrinter = null; BufferedWriter writer = newBufferedWriter(Paths.get(CSV_FILE)); boolean isFirstFragment = true; BigDecimal time_plus = BigDecimal.ZERO; while (sc.hasNext()) { String nextLine = sc.nextLine(); if (nextLine.contains(TIME_OF_START)) { time_plus = new BigDecimal((nextLine.split(" "))[0]); } else if (nextLine.startsWith(STARTS_OF_FRAGMENT) && isFirstFragment) { csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(nextLine.split(DELIMETER))); isFirstFragment = false; } else if (csvPrinter != null) { String[] split = nextLine.split("\\t"); if (split[0].isEmpty()) { time_plus = BigDecimal.ZERO; continue; } try { BigDecimal sumOfFragments = new BigDecimal(split[0]).add(time_plus); split[0] = sumOfFragments.toString(); csvPrinter.printRecord(split); } catch (NumberFormatException ex) { continue; } } } csvPrinter.flush(); } } 
  • Everything is so, only it turns out that each fragment has its own file, but it should be all in one, and for the rest, yes, you are right, but I have made it ... - Vqq50
  • Ah .. well, I thought that the file is final ... but it's already easy there. Take out just higher initialization BufferedWriter + check for filling header'a. - Mikita Berazouski
  • Okay, and if you want the value given in the file description to be added to the 1st column, I correctly understand that the lines will have to be split not in the file itself, but earlier? I tried to do this by creating an array. - Vqq50 pm
  • Give an example where to add and where to insert? - Mikita Berazouski
  • We have 1 column consists of time (ms), the time value from the description should be added to it (each fragment has an individual). For example: "604776 <Fragment start time from the beginning of the study, in ms>" - this parameter is for the fragment, it means that 1 column should be instead of 0 - 604776, instead of 4 - 604780 and so on - Vqq50