如何在Java中检测EOF? [英] How to detect EOF in Java?

查看:160
本文介绍了如何在Java中检测EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些方法来检测代码中的EOF,但仍然无法正常工作。
我尝试使用BufferedReader,Scanner和char u001a标记EOF,但对我的代码仍然没有任何意义。
这是我的最后一个代码:

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");
    }

该程序应该在到达EOF时停止运行,但是我不知道为什么,由于某些原因,该程序一直运行并导致运行时错误。 。
顺便说一句,我是新来的,如果我的问题不太清楚,不好意思,
谢谢:)

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

推荐答案

由于未加密,它一直运行完整的EOF。在流结束时:

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


  1. read()返回-1。

  2. read(byte [])返回-1。

  3. read( byte [],int,int)返回-1。

  4. readLine()返回null。

  5. readXXX()其他X引发 EOFException

  6. Scanner.hasNextXXX()对于任何X返回false。

  7. Scanner。 nextXXX()对任何X抛出 NoSuchElementException

  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.

除非您遇到其中之一,否则您的程序不会遇到流结束。注意 \u001a 是Ctrl / z。不是EOF。 EOF不是字符值。

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.

这篇关于如何在Java中检测EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆