输入时检测到文件结尾 - 请帮助

时间:2012-07-16 18:47:51

标签: java file

我尝试提交此内容并弹出错误,提示输入时检测到 ** END OF FILE - EXIT **

我无法弄清楚这意味着什么,只是为了清楚我正在尝试建立一个系统,以便在循环上输入数据,除非值为0.谢谢。

这是我的代码:

class Main
{
    public static void main( String args[] )
    {
        int SN = 1;
        while ( SN != 0)
        {
            System.out.print("#SN : " );
            SN = BIO.getInt();
            System.out.print("#CW : " );
            int CW = BIO.getInt();
            System.out.print("#EX : " );
            int EX = BIO.getInt();
            double Mark = CW + EX;
            System.out.printf("SN= " + SN + "EX= " + EX + "CW= " + CW + "Mark= " + "%6.1f", (double) Mark / 2      );
        }
    }
}

这是BIO代码:

class BIO
{
  private static final int EOF  = -1;
  private static final int NONE = -2;
  private static int nextChar = NONE;
  private static boolean EOFdetected = false;

  public static String getLineBASE()
  {
    String line = "";                                 // Line read 
    int    ch;                                        // Read ch 

    try
    {
      ch = System.in.read();                          // No next char 

      if ( ch == EOF ) 
      {
        System.out.println("**** END OF FILE " + 
                           "detected on input -- EXIT ****" );
        System.exit(-1);
      }
      while( ch != '\n'  )                            // Read loop 
      {
        if ( ch == EOF )
        {
          EOFdetected = true;
          return line;                                //  exit 
        }
        line = line + (char) ch;                      //  form line 
        ch = System.in.read();                        //  next ch 
      }
      return line;                                    // return line 
    }
    catch( IOException exp )                          // Problem 
    {
      System.exit(-1);                                // Exit ** 
    }
    return "";                                        // Blank line 
  }

 public static String getLine()
  {
    String line = getLineBASE();                      // Read line 
    //System.out.println( line ); 
    return line;
  }


  public static String getString()
  {
    String line = getLine();                          // Read line 
    return line.trim();
  }

  public static double getDouble()
  {
    String res = getLine();                           // Read line 
    double value = 0.0;                               // 
    try
    {
      value = Double.parseDouble( res.trim() );       // Convert 
    }
    catch ( NumberFormatException ex )                // Problem 
    {                                                 //  ignore 
    }
    return value;                                     // return 
  }

  public static int getInt()
  {
    String res = getLine();                           // Read line 
    int value = 0;                                    // 
    try
    {
      value = Integer.parseInt( res.trim() );         // Convert 
    }
    catch ( NumberFormatException ex )                // Problem 
    {                                                 //  ignore 
    }
    return value;                                     // return 
  }

}

1 个答案:

答案 0 :(得分:0)

 try
{
  ch = System.in.read();  //reads from keyboard.

  if ( ch == EOF ) 
  {
    System.out.println("**** END OF FILE " + 
                       "detected on input -- EXIT ****" );
    System.exit(-1);
  }
}

此代码段从键盘读取输入。如果你想从文件中读取东西(我假设你是,因为有一个EOF)查找Scanner类。如果这不是你想要的,我道歉,我只是在假设。