home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-07-17 | 1.8 KB | 95 lines |
- public class basicinput
- {
- public static void main (String args[])
- {
- String s = inString ("Give me a string: ");
- System.out.println ("You typed " + s);
-
- int i = inInt ("Now an int: ");
- System.out.println ("You typed " + i);
-
- char c = inChar ("Now a char: ");
- System.out.println ("You typed " + c);
- }
-
- public static void inputFlush ()
- {
- int dummy;
- int bAvail;
-
- try
- {
- while((System.in.available()) != 0)
- dummy = System.in.read();
- }
- catch(java.io.IOException e)
- {
- System.out.println("Input error");
- }
- }
-
- public static void printPrompt (String prompt)
- {
- System.out.print (prompt);
- }
-
- public static char inChar (String prompt)
- {
- int aChar = 0;
-
- inputFlush ();
- printPrompt (prompt);
-
- try
- {
- aChar = System.in.read();
- }
-
- catch(java.io.IOException e)
- {
- System.out.println("Input error");
- }
-
- inputFlush();
- return (char) aChar;
- }
-
- public static String inString (String prompt)
- {
- int aChar;
- String s = "";
- boolean finished = false;
-
- printPrompt (prompt);
-
- while(!finished)
- {
- try
- {
- aChar = System.in.read();
- if (aChar < 0 || (char)aChar == '\n')
- finished = true;
- else if ((char)aChar != '\r')
- s = s + (char) aChar;
- }
-
- catch(java.io.IOException e)
- {
- System.out.println("Input error");
- finished = true;
- }
- }
- return s;
- }
-
- public static int inInt (String prompt)
- {
- return Integer.valueOf (inString(prompt).trim()).intValue();
- }
-
- public static double inDouble (String prompt)
- {
- return Double.valueOf (inString(prompt).trim()).doubleValue();
- }
- }
-