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

📄 userinput.java

📁 This is the compiled java norms, these norms of a good programmer is very often used
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;

import java.text.DecimalFormat;
import java.util.Scanner;

//##############################################################################
/**
 * The user input class facilitates simple output and input methods in Java that
 * utilise the command line.<br><br>
 * This class facilitates the following fuctions<br><br>
 * <ul>
 * <li>Output to screen
 * <li>Output to file
 * <li>Read from keyboard
 * <li>Read from file
 * <li>Format the output of integer, double and float types (alignment, decimal 
 * places).
 * </ul>
 * 
 * <b>Version: 1.0</b> -  Andrew Scott 2008 
 */
 //#############################################################################
public class UserInput {

	

    private static Scanner kbInput =  new Scanner(System.in);
    private static Scanner input;
    private static File fileInput;
    private static File outputFile;
    
    private static  FileWriter fw;
    private static PrintWriter pw;
    
    public UserInput() { };
    
    
    //==========================================================================
    
    //==========================================================================
    private static Scanner createInput() 
    {
        if (input == null)
         input = new Scanner(System.in);
        return input;
    }//end create input=========================================================
    

    //==========================================================================
    /**
     * Prompts the specified String to the screen, without end-of-line
     * @param s The string to be output.
     */
    //==========================================================================
    public static void prompt (String s) 
    {

            System.out.print(s + " ");
            System.out.flush();

    } // end prompt()===========================================================
    
    //==========================================================================
    /**
    * Prints the specified String to the screen or a file, without end-of-line.
    * <br><br>
    * If the outputFile has been created and the PrintWriter and FileWriter 
    * initialised by the openOutputFile(String name) method then this method 
    * write to the file specified.
    * <br><br>
    * If the outputFile, printWriter and fileWriter objects are null the print
    * method will output to the screen.
    * @param s The string to be output.
    */
    //==========================================================================
    public static void print (String s)
    { 
        if (pw == null || fw == null || outputFile == null)
        {
          System.out.print(s);
          System.out.flush();
        }//end if
        else if (pw != null && fw != null && outputFile != null)
        {
            pw.print(s);
        }//end else if
    } // end prompt()===========================================================
    
    //==========================================================================
    /**
     * Prints the specified String to the screen or a file, with an end-of-line.
     * <br><br>
     * If the outputFile has been created and the PrintWriter and FileWriter 
     * initialised by the openOutputFile(String name) method then this method 
     * write to the file specified.
     * <br><br>
     * If the outputFile, printWriter and fileWriter objects are null the print
     * method will output to the screen.
    * @param s The string to be output.
    */
    //==========================================================================
    public static void println (String s) 
    {
        if (pw == null || fw == null || outputFile == null)
        {
          System.out.println(s);
          System.out.flush();
        }//end if
        else if (pw != null && fw != null && outputFile != null)
        {
            pw.println(s);
        }//end else if
    } // end prompt()===========================================================
    
    /**
     * Prints an int to the screen or file with newLine.
     * @param i the data to be printed
     * @see println(String s)
     */
    public static void println(int i)  {  println( new Integer(i).toString()); }
    
    /**
     * Prints a long to the screen or file with newLine.
     * @param l the data to be printed
     * @see println(String s)
     */
    public static void println(long l) { println(new Long(l).toString());}
    
    /**
     * Prints a short to the screen or file with newLine.
     * @param s the data to be printed
     * @see println(String s)
     */
    public static void println(short s) { println(new Short(s).toString());}
    
    /**
     * Prints a double to the screen or file with newLine.
     * @param d the data to be printed
     * @see println(String s)
     */
    public static void println(double d) { println(new Double(d).toString());}
    
    /**
     * Prints a float to the screen or file with newLine.
     * @param f the data to be printed
     * @see println(String s)
     */
    public static void println(float f) { println(new Float(f).toString()); }
    
    /**
     * Prints a char to the screen or file with newLine.
     * @param c the data to be printed
     * @see println(String s)
     */
    public static void println( char c)  { println(new String(""+ c));}
    
    /**
     * Prints a boolean to the screen or file with newLine.
     * @param b the data to be printed
     * @see println(String s)
     */
    public static void println(boolean b) { println(new Boolean(b).toString());}
    
    /**
     * Prints an integer to the screen or file without newLine.
     * @param i the data to be printed
     * @see print(String s)
     */
    public static void print(int i)  { print( new Integer(i).toString());}
    
    /**
     * Prints a long to the screen or file without newLine.
     * @param l the data to be printed
     * @see print(String s)
     */
    public static void print(long l) { print(new Long(l).toString());}
    
    /**
     * Prints an short to the screen or file without newLine.
     * @param s the data to be printed
     * @see print(String s)
     */
    public static void print(short s) { print(new Short(s).toString());}
    
    /**
     * Prints an double to the screen or file without newLine.
     * @param d the data to be printed
     * @see print(String s)
     */
    public static void print(double d) { print(new Double(d).toString());}
    
    /**
     * Prints an float to the screen or file without newLine.
     * @param f the data to be printed
     * @see print(String s)
     */
    public static void print(float f) { print(new Float(f).toString());}
    
    /**
     * Prints an char to the screen or file without newLine.
     * @param c the data to be printed
     * @see print(String s)
     */
    public static void print( char c)  { print(new String(""+ c));}
    
    /**
     * Prints an boolean to the screen or file without newLine.
     * @param b the data to be printed
     * @see print(String s)
     */
    public static void print(boolean b) { print(new Boolean(b).toString());}
    

    //==========================================================================
    /**
     * Reads input from the keyboard or file and returns it as a char
     * @return The user input value
     */
    //==========================================================================
    public static char readChar() 
    {
        char returnValue = ' ';
        createInput();//Sets up Scanner object

        try 
        {  
            String userInput = new String(input.nextLine());
            returnValue = userInput.charAt(0); //return just the first character 
        }//end try
        catch(Exception e) 
        {
          System.out.println("Exception while reading user's input as a char");	
        }//end catch

    return returnValue;
    } // end readChar()=========================================================

    //==========================================================================
    /**
     * Reads input from the keyboard or file and returns it as a String
     * @return The user input value
     */
    //==========================================================================
    public static String readString() 
    {
        createInput();//Sets up Scanner object
        String userInput = null;

        try 
        {
            userInput = new String(input.nextLine());          
        }//end try
        catch(Exception e) 
        {
            System.out.println("Exception while reading user's input as a " +
                              "String");	
        }//end catch

    return userInput;
    } // end readString()=======================================================


    //==========================================================================
    /**
     * Reads input from the keyboard or file and returns it as a short
     * @return The user input value
     */
    //==========================================================================
    public static short readShort() 
    {
        short returnValue = 0;
        createInput();//Sets up Scanner object
        try 
        {   
          String userInput = new String(input.nextLine());
          returnValue = (java.lang.Short.valueOf(userInput)).shortValue();
        }//end try
        catch(Exception e) 
        {
          System.out.println("Exception while reading user's input as a short");	
        }//end catch

    return returnValue;
    } // end readShort()========================================================

    //==========================================================================
    /**
     * Reads input from the keyboard or file and returns it as an int
     * @return The user input value
     */
    //==========================================================================
    public static int readInt() 
    {
        int returnValue = 0;
        createInput();//Sets up Scanner object
        try 
        {
          String userInput = new String(input.nextLine());
          returnValue = new Integer(userInput).intValue();          
        }//end try
        catch(Exception e) 
        {
           System.out.println("Exception while reading user's input as an int");
           e.printStackTrace();
        }//end catch

    return returnValue;
    } // end readInt()==========================================================

    //==========================================================================
    /**
     *  Reads input from the keyboard or file and returns it as a float
     * @return The user input value
     */
    //==========================================================================
    public static float readFloat() 
    {
        float returnValue = 0;
        createInput();//Sets up Scanner object
        try 
        {
            String userInput = new String(input.nextLine());
            returnValue = (java.lang.Float.valueOf(userInput)).floatValue();          
        }//end try
        catch(Exception e) {
           System.out.println("Exception while reading user's input as a " +
                              "float");	
           e.printStackTrace();
        }//end catch

    return returnValue;
    } // end readFloat()========================================================


    //==========================================================================
    /**
     * Reads input from the keyboard or file and returns it as a double
     * @return The user input value
     */
    //==========================================================================
    public static double readDouble() 
    {
        double returnValue = 0;
        createInput();//Sets up Scanner object
        try 
        {
            String userInput = new String(input.nextLine());
            returnValue = (java.lang.Double.valueOf(userInput)).doubleValue();            
        }//end try
        catch(Exception e) 
        {
           System.out.println("Exception while reading user's input as a " +
                              "double");	
        }//end catch

    return returnValue;
    } // end readDouble()=======================================================


    
    //==========================================================================
    /**
    * Reads input from the keyboard or file and returns it as a long
    * @return The user input value
    */
    //==========================================================================
    public static long readLong() 
    {
        long returnValue = 0;

⌨️ 快捷键说明

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