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 :)
The function feof() is used to check the end of file after EOF. It tests the end of file indicator. It returns non-zero value if successful otherwise, zero.
The eof() function is used to check if the End Of File (EOF) is reached. It returns 1 if EOF is reached or if the FileHandle is not open and undef in all other cases.
End Of File or EOF is a specific designation for a file marker that indicates the end of a file or data set.
It keeps running because it hasn't encountered EOF. At end of stream:
read()
returns -1.read(byte[])
returns -1.read(byte[], int, int)
returns -1.readLine()
returns null.readXXX()
for any other X throws EOFException
.Scanner.hasNextXXX()
returns false for any X.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With