12

I've tried some ways to detect EOF in my code, but it still not working. I've tried using BufferedReader, Scanner, and using char u001a to flag the EOF, but still not make any 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 supposed to stopped when it's already reached the EOF, but I don't know why, for some reasons it keeps running and result a runtime error. Please help. By the way I'm new here, sorry if my question is not really clear to be understood, Thank you before :)

Bertha Alan
  • 131
  • 1
  • 1
  • 6
  • 2
    What is the Runtime error that you are getting?.. – TheLostMind Apr 09 '16 at 11:20
  • Also, check [this](http://stackoverflow.com/questions/16104616/using-bufferedreader-to-read-text-file) post on how to read a file using `FileReader` + `BufferedReader` – TheLostMind Apr 09 '16 at 11:21
  • I believe the scanner will just return ```null``` instead of an EOF character. That would explain the runtime exception. – Jorn Vernee Apr 09 '16 at 11:45
  • @JornVernee No hasNextLine will check the underlying read for null and return false. His program works as expected on my machine. – totoro Apr 09 '16 at 11:48
  • You are reading input from the console (stdin), maybe it behaves strange on your host. Try reading from a file you open. – totoro Apr 09 '16 at 11:50
  • how do you run your program? are you actually piping in content from a file or are you just running it without piping in content (and just inputting values by hand into the console)? – Michael Ritter Jan 24 '17 at 17:29

4 Answers4

20

It keeps running because it hasn't encountered EOF. At end of stream:

  1. read() returns -1.
  2. read(byte[]) returns -1.
  3. read(byte[], int, int) returns -1.
  4. readLine() returns null.
  5. readXXX() for any other X throws EOFException.
  6. Scanner.hasNextXXX() returns false for any X.
  7. Scanner.nextXXX() throws NoSuchElementException for any X.

Unless you've encountered one of these, your program hasn't encountered end of stream. NB \u001a is a Ctrl/z. Not EOF. EOF is not a character value.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • It might be worth pointing out that you need to use the corresponding `hasNext...` call, not just `hasNextLine` everywhere, since that will return true if you read the last token with `next` and there's an empty line at the end. – Bernhard Barker Dec 17 '17 at 16:17
3

This is what I did

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

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

Worked for me

0

Specifically for System.in you should be able to check whether the backing FileDescriptor is still open via FileDescriptor.in.valid().

Simple demo:

$ cat FD.java
public class FD {
  public static void main(String[] args) throws Exception {
    System.out.println(java.io.FileDescriptor.in.valid());
    System.in.close();
    System.out.println(java.io.FileDescriptor.in.valid());
  }
}

$ javac FD.java
$ java -cp . FD
true
false
dimo414
  • 47,227
  • 18
  • 148
  • 244
-3

You can use try and catch.

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){

}

Here, when you will give Ctrl+z, Scanner.nextLine() will give you NoSuchElementException. Use this exception as a condition of EOF. To handle this exception use try and catch.

Sadi Hurayv
  • 21
  • 1
  • 1
  • 3
  • 1
    Hacky - it ignores the hasNextLine method intended to tell you when input is exhausted. –  May 07 '19 at 01:25
  • Incorrect. `nextLine()` doesn't return null at EOS, it throws `NoSuchElementException`. – user207421 Nov 30 '19 at 22:18