Friday, 29 June 2012

Reading Input From Terminal Or DOS Using Input Java Class



Program is often run and display using command line (dos) and interacts with the user or an outside environment. Java language supports this kind of interaction in two ways: through the Standard Streams and through the Console. Using this kind of method is not usually intended for the newbie to start learning programming.

Here's a class for reading input from terminal or dos developed by Shiegfred L. Tan a teacher from University of St. La Salle, Philippines. A class designed to make our program interact with the user easily. Reading keyboard input and parse value per line. If the user enters an improper input, ex. an input of the wrong type, misspelled keyword, a blank line, then the user is prompted to reenter the input and given a brief explanation of what is required. Also includes some additional methods to input single numbers, words, and characters, without going to the next line.

Usage:
01public class Usage {
02  public static void main(String [] args) {
03    // for string input
04    String s;
05    System.out.print("Enter a string value: ");
06    s = Input.readString();
07    System.out.println(s);
08 
09    // for integer input
10    int i;
11    System.out.print("Enter an integer value: ");
12    i = Input.readInt();
13    System.out.println(i);
14 
15    // for double input
16    double d;
17    System.out.print("Enter a double value: ");
18    d = Input.readDouble();
19    System.out.println(d);
20 
21    // for char input
22    char c;
23    System.out.print("Enter a char value: ");
24    c = Input.readChar();
25    System.out.println(c);
26 
27  }
28}

0 comments:

Post a Comment

Internet-PublicLab. Powered by Blogger.