Tag Archives: Scanner

JAVA: scanner Read File encoding Error noSuchElementException

I need to demonstrate to customers locally importing data from postgresql into elasticsearch. I wrote a program read3.java in idea. There is no problem reading the file content with scanner.

But this read3.java is only used to convert the json file exported by the table into a valid json file that can be imported into es, and does not add the dependency that needs to be downloaded, but I have to do it in the idea every time I run it.

So I directly found the target path of read3.java and copied it to the desktop.

Compile a .class file with javac directly on the desktop and then start running.

Every time there is an error, that is

Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)

……

Then I reopened the notepad and created an abc.json file, saved the encoding format as “utf-8”, and then used read3 to read the file, and the read out was garbled.

So I thought that there is a problem with the encoding of the characters read by the scanner. When creating the scanner, specify the encoding, that is, Scanner sc=new Scanner(new FIle(“xxx”),”utf-8″));

Then use notepad++ to open the file that needs to be read, and find that the file encoding is utf-8 bom, so I converted the encoding format to utf-8 and re-run. The problem is solved.

How to Solve Error: Scanner class java. Util. NoSuchElementException exception

Today, when using the scanner class to obtain data from the console, a Java. Util. NoSuchElementException exception was reported

Here is the problem code

public static void init(LinkList l){
        for(int i=0;i<3;i++){
            System.out.println("Please enter the "+i+" value");
            Scanner temp=new Scanner(System.in);
            int len=temp.nextInt();
            l.addNode(len);
            temp.close();
        }
    }

When you want to input the second data, you will report an error. This is abnormal information

Please enter the 0th value
2
Please enter the 1st value
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at com.zji.List.LinkList.init(LinkList.java:33)
    at com.zji.List.LinkList.main(LinkList.java:60)

This is because temp. Close() is closed in the for loop. Just write temp. Close() outside the for loop

At first, I thought I’d cycle through a new scanner and turn it off. Can’t I turn it on again?No! This is because your scanner. Close will call the system. In. Close method to close the system. In. After you, new scanner will not help you open the system. In stream, so an error is reported

The following is the source code of the close method, which you can refer to

public void close() {
      if (closed)
          return;
      if (source instanceof Closeable) {
          try {
              ((Closeable)source).close();
          } catch (IOException ioe) {
              lastException = ioe;
          }
      }
      sourceClosed = true;
      source = null;
      closed = true;
  }