How to detect EOF in Java?

I tried some ways to detect EOF in my code, but it still does not work. I tried using BufferedReader, Scanner and using char u001a to denote EOF, but still makes no sense to my code. Here is my last code:

    Scanner n=new Scanner(System.in);
    String input;
    int counter=0;

    while(n.hasNextLine())
    {
        input=n.nextLine();
        char[] charInput=input.toCharArray();
        for (int i = 0; i < input.length(); i++) {
            if(charInput[i]=='"')
            {
                if(counter%2==0)
                {
                    System.out.print("``");
                }
                else
                {
                    System.out.print("''");
                }
                counter++;
            }
            else
            {
                System.out.print(charInput[i]);
            } 
        }
        System.out.print("\n");
    }

The program should stop when it already reaches EOF, but I don’t know why, for some reason, it continues to work and leads to a runtime error. Please help. By the way, I'm new here, I'm sorry if my question is not entirely clear so that they understand it, Thank you earlier :)

+7
source share
7 answers

It works because it did not come across EOF. At the end of the stream:

  • read() returns -1.
  • read(byte[]) returns -1.
  • read(byte[], int, int) returns -1.
  • readLine() returns null.
  • readXXX() X EOFException.
  • Scanner.hasNextLine() false.
  • Scanner.nextLine() throws NoSuchElementException.

, . NB \u001a - Ctrl/z. EOF. EOF .

+9

GNU/Linux OpenJDK:

java Test <<eof
> """"
> """"""
> eof
``''``''
``''``''``''

stdin (System.in).

0

(ex: < file.txt), (\n), next()/hasNext $(). CTRL + D.

( ) .

String input = in.useDelimiter("\\p{javaWhitespace}*").next();
0

,

Scanner s = new Scanner(f); //f is the file object

while(s.hasNext())
{
String ss = s.nextLine();
System.out.println(ss);
}

0

.

Scanner n=new Scanner(System.in);
String input;
int counter=0;
input=n.nextLine();
try{
    while(input!=null)
    { 
        char[] charInput=input.toCharArray();
        for (int i = 0; i < input.length(); i++) {
            if(charInput[i]=='"')
            {
                if(counter%2==0)
                {
                    System.out.print("''");
                }
                else
                {
                    System.out.print("''");
                }
                counter++;
            }
            else
            {
                System.out.print(charInput[i]);
            } 
        }
        System.out.print("\n");
        input=n.nextLine();
    }
}catch(Exception e){

}

, Ctrl + z, Scanner.nextLine() NoSuchElementException. EOF. , try catch.

0

, , , EOF (ctrl + d ):

String input = n.useDelimiter("\\A").next();

, System.in.

-1

when you enter data from System.in, by default you enter keyboard input, which is an infinite input stream. It contains an infinite number of lines, due to which the while loop does not stop.

I think switching to EOF might work, like CTRL + Z might work

-1
source

All Articles