1

This is a quite simple task and I have done this a lot of times. But, at the moment, I am stuck at this trivial line of code.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test {

private static Scanner scan;

  public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    File file = null;
    switch (1) {
    case 1:
        file = new File("W:\\Umesoft Evobus\\From AQUA\\Aqua data_ All\\20090101-20090630_datenabzug_tilde.txt");
        break;
    case 2:
        file = new File("W:\\Umesoft Evobus\\From AQUA\\Aqua data_ All\\20090701-20091231_datenabzug_tilde.txt");
        break;
    }
    scan = new Scanner(file);
    String x = scan.nextLine();
    System.out.println(x);
  }

}

When I try to read the first file, I get a NoSuchElementException. When I try to read the second file, I have no issues. Both the files are from the same source and have the same format. I am sure, there are no issues with regards to the input files. The first line in both the files are identical.

Can someone explain this situation?

The above program is just for testing. Hence, I have used a switch case to select the file.

In the actual program, a set of files are selected by the user. And every time, this file is being skipped. The input files are data files, generated through another program. They are very similar to CSV files, but the delimiter used here is ~ for some reasons. They cannot be empty, because, even in the worst case, they would have headers.

Screenshot of the file contents in notepad++:

Output for file 1:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at controller.Test.main(Test.java:24)

Output for file 2:

Weltherstellercode~FIN~Fahrzeug_Baumuster~~Motoridentnummer~Getriebe_Identifizierungsnummer~Produktionsdatum~Produktionsnummer_Fzg~Erstzulassungsdatum~Reparaturdatum~Fahrzeug_Laufleistung_in_km~Interne_VEGA_Antragsnummer~TGA~Fehlerort~~Fehlerart~~Reparaturart~~Hauptschadensteil~Reparaturland_(G&K)~~Reparaturbetrieb_(G&K)~~Mitteilungstext~Gutschriftsdatum_(Summe)~Anzahl_Beanstandungen~Gesamtkosten~Lohnkosten~Materialkosten~Summe_DH+NK~Anzahl_Arbeitswerte_(Gutgeschrieben)                                                                      
el.diablo
  • 21
  • 7

2 Answers2

1
       String line ="";
       BufferedReader br = new BufferedReader(new FileReader("path"));
       while ((line = br.readLine()) != null) {
       System.out.println(line);            
       }

i changed previous code to use a buffered reader since

BufferedReader has significantly larger buffer memory than Scanner. Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream

Priyamal
  • 2,919
  • 2
  • 25
  • 52
  • This avoids the exception. But, I cannot read the file. I am forcing the program to read the file, because, in my actual program, the file is selected by the user through a GUI. Its a data file and the first line is supposed to contain the headers. Since these are generated from another program, whether or not data is present, the headers are definitely present. – el.diablo Apr 21 '16 at 09:36
  • if it avoids the exception , but at some point is it printing out your line ? – Priyamal Apr 21 '16 at 09:39
  • Please check out the screenshot that I have added. I know this sounds trivially stupid. – el.diablo Apr 21 '16 at 09:54
  • I didn't want to use BufferedReader. It worked with it though. I did find out the solution from another post. – el.diablo Apr 21 '16 at 11:37
1

The following answer from a different post, worked.

https://stackoverflow.com/a/35173548/6234625

scan = new Scanner(file,"UTF-8");

I had to mention the encoding for the Scanner.

Thanks to everyone who tried to help me. Thanks especially to @Abhisheik and @Priyamal.

Community
  • 1
  • 1
el.diablo
  • 21
  • 7