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

📄 text.java

📁 This is the compiled java norms, these norms of a good programmer is very often used. this ia a test
💻 JAVA
字号:

import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.JOptionPane;

public class Text 
{

  /* The All New Famous Text class     by J M Bishop  Aug 1996
   *            revised for Java 1.1 by Alwyn Moolman Aug 1997
   *            revised for efficiency by J M Bishop Dec 1997
   *
   * Provides simple input from the keyboard and files.
   * Now also has simple output formatting methods
   * and file opening facilities.
   *
   * public static void   prompt (String s)
   * public static int    readInt (BufferedReader in)
   * public static double readDouble (BufferedReader in)
   * public static String readString (BufferedReader in)
   * public static String readLine (BufferedReader in)
   * public static char   readChar (BufferedReader in)
   * public static String writeInt (int number, int align)
   * public static String writeDouble
                   (double number, int align, int frac)
   * public static BufferedReader open (InputStream in)
   * public static BufferedReader open (String filename)
   * public static PrintWriter create (String filename)
   */

  private static StringTokenizer T;
  private static String S;
  private static String textIn;

  public static BufferedReader open(String filename) throws FileNotFoundException
  {
    return new BufferedReader(new FileReader (filename));
  }//end open

  public static PrintWriter create(String filename) throws IOException
  {
    return new PrintWriter(new FileWriter (filename));
  }//end create

  public static void showMessage(String message)
  {
    JOptionPane.showMessageDialog(null, message);
  }//end of showMessage method
  
  public static int readInt(String prompt) 
  {   
      while (true)
      {
        //textIn = JOptionPane.showInputDialog(prompt);
        try
        {
          return Integer.parseInt(JOptionPane.showInputDialog(prompt));
        }//end try
        catch (NumberFormatException e2)
        {
          JOptionPane.showMessageDialog(null, "Error in number, try again");
        }//end catch
      }//end while
  }//end readInt

  public static char readChar(String prompt)
  {   
      while (true)
      {
        textIn = JOptionPane.showInputDialog(prompt);
        if (textIn.length() == 1)
        {
          return textIn.trim().charAt(0);
        }//end if
        else
        {
          JOptionPane.showMessageDialog(null, "One character required, try again");
        }//end else
      }//end while
   }//end readChar

  public static double readDouble(String prompt)
  {   
    while (true)
    
    {
      textIn = JOptionPane.showInputDialog(prompt);
      try
      {
        return Double.valueOf(textIn.trim()).doubleValue();
      }//end try
      catch (NumberFormatException e2)
      {
        JOptionPane.showMessageDialog(null, "Error in number, try again");
      }//end catch
    }//end while
  }//end readDouble

  public static String readString(String prompt) throws IOException
  {
    return JOptionPane.showInputDialog(prompt);
  }//end readString

  public static int readInt(BufferedReader in) throws IOException
  {
      if (T==null)
      {
        refresh(in);
      }//end if
      while (true)
      {
        try
        {
          return Integer.parseInt(T.nextToken());
        }//end try
        catch (NoSuchElementException e1)
        {
          refresh (in);
        }//end catch
        catch (NumberFormatException e2)
        {
          System.out.println("Error in number, try again.");
        }//end catch
      }//end while
   }//end readInt

 public static char readChar (BufferedReader in) throws IOException
 {
      if (T==null)
      {
        refresh(in);
      }//end if
      while (true)
      {
        try
        {
          return T.nextToken().trim().charAt(0);
        }//end try
        catch (NoSuchElementException e1)
        {
          refresh (in);
        }//end catch
      }//end while
   }//end readChar

 public static double readDouble (BufferedReader in) throws IOException
 {
      if (T==null)
      {
        refresh(in);
      }//end if
      while (true)
      {
        try
        {
          String item = T.nextToken();
          return Double.valueOf(item.trim()).doubleValue();
        }//end try
        catch (NoSuchElementException e1)
        {
          refresh (in);
        }//end catch
        catch (NumberFormatException e2)
        {
          System.out.println("Error in number, try again.");
        }//end catch
      }//end while
   }//end readDouble

  public static String readString (BufferedReader in) throws IOException
  {
    if (T==null)
    {
      refresh (in);
    }//end if
    while (true)
    {
      try
      {
        return T.nextToken();
      }//end try
      catch (NoSuchElementException e1)
      {
        refresh (in);
      }//end catch
    }//end while
  }//end readString

  public static String readLine(BufferedReader in) throws IOException
  {
    while (true)
    {
      try
      {
        return in.readLine();
      }//end try
      catch (NoSuchElementException e1)
      {
        refresh (in);
      }//end catch
    }//end while

  }//end readLine

  private static void refresh (BufferedReader in) throws IOException
  {
    S = in.readLine();
    if (S==null)
    {
      throw new EOFException();
    }//end if
    T = new StringTokenizer(S);
  }//end refresh

  //  Number formatting methods
  //  -------------------------

  private static DecimalFormat N = new DecimalFormat();
  private static final String spaces = "                    ";

  public static String formatDouble (double number, int align, int frac)
  {
    N.setGroupingUsed(false);
    N.setMaximumFractionDigits(frac);
    N.setMinimumFractionDigits(frac);
    String num = N.format(number);
    if (align < 0) // true implies left-justify
    {
      if (num.length() < -align)
      {
        num = num + spaces.substring(0,-align-num.length()); // add trailing spaces
      }
    }
    else
    {
      if (num.length() < align)
        num = spaces.substring(0,align-num.length()) + num;
    }
    return num;
  }//end formatDouble

  public static String formatInt (int number, int align)
  {
    N.setGroupingUsed(false);
    N.setMaximumFractionDigits(0);
    String num = N.format(number);
    if (align < 0) // true implies left-justify
    {
      if (num.length() < -align)
      {
        num = num + spaces.substring(0,-align-num.length()); // add trailing spaces
      }
    }
    else
    {
      if (num.length() < align)
        num = spaces.substring(0,align-num.length()) + num;
    }
    return num;
  }//end formatInt

  public static void main(String[] args) throws IOException
  {
    int             integerNumber;
    double          doubleNumber;
    char            singleChar;
    String          aString;

    System.out.println();
    System.out.println();
    System.out.println();

    integerNumber = Text.readInt("Input an integer number");    // read integer number
    System.out.println(integerNumber);                          // output integer number read
    System.out.println(Text.formatInt(integerNumber, 4) + "|");       // output integer number read right-justified in a field width 4
    System.out.println(Text.formatInt(integerNumber, -4) + "|");      // output integer number read left-justified in a field width 4
    
/*

If the integer number 23 is input, the output will be:

23
  23
23

If the integer number -32 is input, the output will be:

-32
 -32
-32

*/

    System.out.println();
    System.out.println();
    System.out.println();

    doubleNumber = Text.readDouble("Input a double number");    // read double number
    System.out.println(doubleNumber);                           // output double number read
    System.out.println(Text.formatDouble(doubleNumber, 8, 3) + "|");  // output double number read right-justified in a field width 8 with three decimal places
    System.out.println(Text.formatDouble(doubleNumber, -8, 3) + "|"); // output double number read left-justified in a field width 8 with three decimal places

/*

If the double number 4.1237 is input, the output will be: 

4.1237
   4.124
4.124

If the double number .02367 is input, the output will be:

0.02367
   0.024
0.024

*/

    System.out.println();
    System.out.println();
    System.out.println();

    aString = Text.readString("Input a string");                // read a string from keyboard
    System.out.println(aString);                                // output string

    System.out.println();
    System.out.println();
    System.out.println();

    singleChar = Text.readChar("Input a single character");     // read a single character from keyboard
    System.out.println(singleChar);                             // output the character

/*


Input a single character: y
y

Input a single character: n
n

*/
/*
  PrintWriter outFile;

  outFile = Text.create("f:/se1s01-2004/WorksheetsWS/Text files/output.txt");
  for (int i = 1; i < 5; i++)
  {
    outFile.println(i);
  }//end of for
  outFile.println(0);
  outFile.close();


Listing of the file 'output.txt'

1
2
3
4
0


  outFile = Text.create("f:/se1s01-2004/WorksheetsWS/Text files/output.txt");
  for (int i = 1; i < 8; i++)
  {
    outFile.println(i);
  }//end of for
  outFile.println(0);
  outFile.close();


The file 'output.txt' which must be stored in the 'Text files' folder
1
2
3
4
5
6
7
0


  BufferedReader inputFile;

  inputFile = Text.open("f:/se1s01-2004/WorksheetsWS/Text files/output.txt");
  int value = Text.readInt(inputFile);
  while (value != 0)
  {
    System.out.println(value);
    value = Text.readInt(inputFile);
  }//end while
  inputFile.close();

  try
  {
    while (true)
    {
      int value = Text.readInt(inputFile);
      System.out.println(value);
    }//end while
  }
  catch(EOFException e)
  {
  }//end catch
  inputFile.close();

Output produced by this segment of code
1
2
3
4
5
6
7
*/

    System.exit(0);
  }//end of main method

}//end of class Text

⌨️ 快捷键说明

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