Java logo

PIO



/** pio.java
    @author Dr. William J. Pervin
    @version 1.0
    Adapted from Savitch's classes
    Simple, consistent classes for input of primitives
*/
import java.io.*;
import java.util.*;

public class pio
{
    public static char getChar()
    {
        int charAsInt = -1;
        try
        {       charAsInt = System.in.read();
        }
        catch(IOException e)
        {
                System.out.println("Error: " + e.getMessage());
                System.out.println("Fatal error; ending program!");
                System.exit(1);
        }
        return (char)charAsInt;
    }

    public static String getWord()
    {
        String result = "";
        char next;

        next = getChar();
        while (Character.isWhitespace(next)) next = getChar();

        while (!(Character.isWhitespace(next)))
        {   result = result + next;
            next = getChar();
        }

        if (next == '\r')
        {   next = getChar();
            if (next != '\n')
            {   System.out.println("Error in getWord of pio");
                System.exit(1);
            }
        }

        return result;
    }

    public static int getInt() throws NumberFormatException
    {
        String inputString = null;
        inputString = getWord();
        return (Integer.valueOf(inputString).intValue());
    }

    public static long getLong() throws NumberFormatException
    {
        String inputString = null;
        inputString = getWord();
        return (Long.valueOf(inputString).longValue());
    }

    public static float getFloat() throws NumberFormatException
    {
        String inputString = null;
        inputString = getWord();
        return (Float.valueOf(inputString).floatValue());
    }

    public static double getDouble() throws NumberFormatException
    {
        String inputString = null;
        inputString = getWord();
        return (Double.valueOf(inputString).doubleValue());
    }

    public static boolean getBoolean() 
    {
        boolean result = false;
        String inputString = null;
        inputString = getWord();
        if (inputString.equalsIgnoreCase("true")
            || inputString.equalsIgnoreCase("T"))
        {
            result = true;
        }
        else
        if (inputString.equalsIgnoreCase("false")
            || inputString.equalsIgnoreCase("F"))
        {
            result = false;
        }
        else
        {   System.out.println("Boolean must be T or F");
            System.exit(1);
        }

        return result;
    }  
}