123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import java.io.*;
- import java.util.*;
- import java.text.*;
-
- public class TextIO {
-
- public TextIO () {};
-
- /* 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 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;
-
- public static BufferedReader open (InputStream in) {
- return new BufferedReader(new InputStreamReader(in));
- }
-
- public static BufferedReader open (String filename)
- throws FileNotFoundException {
- return new BufferedReader (new FileReader (filename));
- }
-
- public static PrintWriter create
- (String filename) throws IOException {
- return new PrintWriter (new FileWriter (filename));
- }
-
- public static void prompt (String s) {
- System.out.print(s + " ");
- System.out.flush();
- }
-
- public static int readInt (BufferedReader in) throws IOException {
- if (T==null) refresh(in);
- while (true) {
- try {
- return Integer.parseInt(T.nextToken());
- }
- catch (NoSuchElementException e1) {
- refresh (in);
- }
- catch (NumberFormatException e2) {
- System.out.println("Error in number, try again.");
- }
- }
- }
-
- public static char readChar (BufferedReader in) throws IOException {
- if (T==null) refresh(in);
- while (true) {
- try {
- return T.nextToken().trim().charAt(0);
- }
- catch (NoSuchElementException e1) {
- refresh (in);
- }
- }
- }
-
- public static double readDouble (BufferedReader in) throws IOException {
- if (T==null) refresh(in);
- while (true) {
- try {
- String item = T.nextToken();
- return Double.valueOf(item.trim()).doubleValue();
- }
- catch (NoSuchElementException e1) {
- refresh (in);
- }
- catch (NumberFormatException e2) {
- System.out.println("Error in number, try again.");
- }
- }
- }
-
- public static String readString (BufferedReader in) throws IOException {
- if (T==null) refresh (in);
- while (true) {
- try {
- return T.nextToken();
- }
- catch (NoSuchElementException e1) {
- refresh (in);
- }
- }
- }
-
- // Keep reading.
- private static void refresh (BufferedReader in) throws IOException {
- S = in.readLine ();
- if (S==null) throw new EOFException();
- T = new StringTokenizer (S);
- }
-
- // Write methods
- // -------------
-
- private static DecimalFormat N = new DecimalFormat();
- private static final String spaces = " ";
-
- public static String writeDouble (double number, int align, int frac) {
- N.setGroupingUsed(false);
- N.setMaximumFractionDigits(frac);
- N.setMinimumFractionDigits(frac);
- String num = N.format(number);
- if (num.length() < align)
- num = spaces.substring(0,align-num.length()) + num;
- return num;
- }
-
- // Okay to finish this lab, create a class that uses a few of the methods
- // here. Make sure it runs. Commit the changes and do a pull request.
- public static String writeInt (int number, int align) {
- N.setGroupingUsed(false);
- N.setMaximumFractionDigits(0);
- String num = N.format(number);
- if (num.length() < align)
- num = spaces.substring(0,align-num.length()) + num;
- return num;
-
- }
- }
|