Пример #1
0
  /** reads an char from InputStream in */
  public static char readChar(InputStream in) {
    char result = '?';
    DataInputStream input = new DataInputStream(in);

    String line;
    try {
      line = input.readLine();
    } catch (IOException e) {
      System.out.println("readChar: couldn't read line.");
      return '?';
    }
    try {
      result = line.charAt(0);
    } catch (Exception e) {
      System.out.print("readChar: couldn't convert input to char, try again: ");
      result = readChar(in);
    }
    //        System.out.println("answer is " + result);

    if (_out != null) // echo result to file
    {
      _out.println(result);
      _out.flush();
    }

    return result;
  } // readChar
Пример #2
0
  /** reads an int from InputStream in */
  public static int readInt(InputStream in) {
    int result = -1;
    DataInputStream input = new DataInputStream((in));
    String line;

    try {
      line = input.readLine();
    } catch (IOException e) {
      System.out.println("readInt: couldn't read line.");
      return -1;
    }

    // System.out.println("line is: " + line);
    try {
      result = Integer.parseInt(line);
    } catch (NumberFormatException e) {
      System.out.print("readInt: couln't convert input to int, try again: ");
      result = readInt(in);
    }
    // System.out.println("answer is " + result);

    if (_out != null) // echo result to file
    {
      _out.println(result);
      _out.flush();
    }

    return result;
  } // readInt