⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formattedinput.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
import java.io.*;

public class FormattedInput
{
  // Method to read an int value LISTED BELOW
  // Method to read an int value
  public int intRead()
  {
    try
    {
      for(int i = 0; i < 5; i++)
      {
        if(tokenizer.nextToken() == tokenizer.TT_NUMBER && 
                                           tokenizer.nval == (double)((long) tokenizer.nval))
          return (int)tokenizer.nval;    // Value is numeric, so return as int 
        else
        {
          System.out.println("Incorrect input: " + tokenizer.sval +
                             " Re-enter an integer");
          continue;                      // Retry the read operation
        }
      }  
      System.out.println("Five failures reading an int value" +
                                        " - program terminated");
      System.exit(1);                    // End the program
      return 0;
    }
    catch(IOException e)                 // Error reading in nextToken()
    {
      System.out.println(e);             // Output the error
      System.exit(1);                    // End the program
      return 0;
    }
  }

  // plus methods to read various other data types

  // Read a  string
  public String stringRead()
  {
    try
    {
      for(int i = 0; i < 5; i++)
      {
        int tokenType = tokenizer.nextToken();        // Read a token
        if(tokenType==tokenizer.TT_WORD || tokenType == '\"')   // Type is a string
          return tokenizer.sval;                                // so return it 
        else if(tokenType == '!')                     // Non-alpha returned as type
          return "!";                                 // so return end string
        else
        {
          System.out.println(
                    "Incorrect input. Re-enter a string between double quotes");
          continue;            // Retry the read operation
        }
      }  
      System.out.println("Five failures reading a string" +
                                        " - program terminated");
      System.exit(1);          // End the program
      return null;
    }
    catch(IOException e)       // Error reading in nextToken()
    {
      System.out.println(e);   // Output the error
      System.exit(1);          // End the program
      return null;
    }
  }

  

  // Object to tokenize input from the standard input stream
  private StreamTokenizer tokenizer = new StreamTokenizer(
                                      new InputStreamReader(System.in));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -