Q: Exception in thread "main" NoSuchElementException I have problem in my code. There are 3 classes and text file: Main, Listener, Unduplicator and Dictionary.txt. If I enter word program has to check is that word exist in Dictionary.txt. If not, it has to save that word in Dictionary.txt. I made something wrong and after I write word it crash and show info 
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at FIMP.Listener.TextFrom(Listener.java:8)
at FIMP.Unduplicator.<init>(Unduplicator.java:13)
at FIMP.Main.main(Main.java:11

I have no idea how I can fix it. 
Class Main

    package FIMP;

    import java.io.IOException;

    public class Main {
    public static void main (String args[]) throws IOException{

        Listener listener = new Listener();
        listener.TextFrom();

    Unduplicator unduplicator = new Unduplicator();

    }
    }

Class Listener

    package FIMP;

    import java.util.*;

    public class Listener{
    public String TextFrom(){
        Scanner TextScan = new Scanner(System.in);
        String ST = TextScan.next();
        TextScan.close();
        return ST;
    }
}

Class Unduplicator
package FIMP;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Unduplicator {

public Unduplicator() throws IOException{

    Listener listener = new Listener();

    String word = listener.TextFrom();
    Scanner scanner = new Scanner("Dictionary.txt");

    while (scanner.hasNextLine()) {
        String nextToken = scanner.next();
        if (nextToken.equalsIgnoreCase(word)){
        System.out.println("Word EXIST");
    }
        else{
            FileWriter TextStream = new FileWriter("Dictionary.txt", true);
            TextStream.write(listener.TextFrom() + "\n");
            TextStream.close();
        }
   scanner.close();       
}
}
}


A: The problem is that you do the following:
 Scanner TextScan = new Scanner(System.in);
 ...
 TextScan.close();

When calling close() you also close the underlying stream. In this case: System.in. This is usually not recommended. Use instead only a single instance of Scanner and don't close it.

A: In you main method your are calling
Listener listener = new Listener();
listener.TextFrom();

which closes System.in
and then in the Unduplicator you are trying to call
Listener listener = new Listener();
String word = listener.TextFrom();  // but System.in is closed

