Keyboard Class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Keyboard Class

  1. #1
    Join Date
    Oct 2004
    Posts
    18

    Keyboard Class

    hi all,

    I need to write a program that prompt the user to enter a number(single digit) one at a time (one per line, 5 times). Insert these 5 number into an array. And then output them on the screen.

    I am having problem of using Keyboard class. Does anyone have any idea how to do this or perhaps a sample code would be greatly appreciated.

    Thanks!

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    There is no Keyboard class in the standard java library. Did you get this class from your school or university? Show us the class and we'll be able to see what it's doing.

  3. #3
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    I don't know why you wana be using a array for that
    in my understanding you're having trouble with
    in and out
    well try this work on it
    you'll probably make it to where you heading
    good luck


    --------------------------------------------
    import java.io.*;

    class inout{
    static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] lalama)throws IOException{

    System.out.println("Enter a 5 digit number");

    int P;

    P=Integer.parseInt(in.readLine());
    String s=""+P+"";
    if(s.length()<87){
    System.out.println("here's what you gave"+s);
    }
    else{System.out.println("I only deal with less than or aqual to 5 digits ");
    }

    }
    }

  4. #4
    Join Date
    Oct 2004
    Posts
    18
    The reason that I wanted to these integers for otherpurposes. But for now, all I'd like to be able to do is as mentioned above.

    Thanks!

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    If you specifically need to use the class called Keyboard we can't help you yet because we don't know what it is. You'll have to post it here so we can read it.

    If it is a class for you to be able to get information from the user using the keyboard, then it will likely be doing pretty much what the code that Peter wrote is doing.

  6. #6
    Join Date
    Oct 2004
    Posts
    18
    Hi guys,

    I finally found a keyboard class from the university. Now, what do i need to do to achieve what i've said (above). The code of the keyboard class as the following:

    import java.io.*;

    public class Keyboard extends www {
    /*
    An instance of this class represents a keyboard device that can be
    used to obtain input from the user.
    */

    /* Public Variables */

    public static final Keyboard in = new Keyboard();

    /* Contructor */

    public Keyboard() {

    /*
    Initialize me.
    */
    this.reader = new BufferedReader(new InputStreamReader(System.in));
    }

    /* Instance Methods */

    public String readString() {
    /*
    Answer a String that contains all of the characters typed by the
    user until the enter key is pressed.
    */
    String aString;

    System.out.flush(); // Make sure all output is flushed
    try {
    aString = this.reader().readLine();
    } catch (Exception e) {
    aString = "";
    }
    return aString;
    }

    public Integer readInteger() {
    /*
    Answer an Integer that is represented by the String that
    contains all of the characters typed by the user until the
    enter key is pressed. If the text does not form a valid
    Integer, then answer null.
    */

    String aString;
    Integer anInteger;

    System.out.flush(); // Make sure all output is flushed
    aString = this.readString();
    try {
    anInteger = new Integer(aString);
    } catch (Exception e) {
    anInteger = null;
    }
    return anInteger;
    }

    public Float readFloat() {
    /*
    Answer a Float that is represented by the String that
    contains all of the characters typed by the user until the
    enter key is pressed. If the text does not form a valid
    Float, then answer null.
    */

    String aString;
    Float aFloat;

    System.out.flush(); // Make sure all output is flushed
    aString = this.readString();
    try {
    aFloat = new Float(aString);
    } catch (Exception e) {
    aFloat = null;
    }
    return aFloat;
    }

    public void pause() {
    /*
    Display a message and pause until the enter key is pressed.
    */
    String aString;

    System.out.print("Press the ENTER key to continue ...");
    System.out.flush(); // Make sure all output is flushed
    try {
    aString = this.reader().readLine();
    } catch (Exception e) {}
    }

    /* Private Instance Variables */

    private BufferedReader reader;

    /* Private Instance Methods - accessing */

    private BufferedReader reader() {
    /*
    Answer my reader.
    */

    return this.reader;
    }

    private void reader(BufferedReader aReader) {
    /*
    Set my reader to the given one.
    */

    this.reader = aReader;
    }

    }
    thanks guys!

  7. #7
    Join Date
    Feb 2004
    Posts
    541
    Ok, the keyboard class is straightforward enough. You don't need to make an instance of it because it has a static instance of itself as a public variable.

    It has various methods that will return certain different types (such as String, Integer, Double etc). For your example you want to get an Integer from the user. When you call each method, your app will wait for the user to enter something on the command line and then press enter/return. When that happens, the method will read in what was entered and return it.

    To get an Integer from the user, use the following code...
    Code:
    Integer anInt = Keyboard.in.readInteger();
    Note, the Keyboard class doesn't return any primitive types, it only returns object types. Integer is a wrapper class for the primitive type int. If the user doesn't enter a valid integer, then the method will return a null object. You need to check for this, otherwise you'll get NullPointerExceptions flying around.

  8. #8
    Join Date
    Oct 2004
    Posts
    18
    Hi,

    thank you very much for all the help (above). I have an other question. How do you check if the user has enter a string or an integer. in other word, anything other than 0 to 9. I am using the following code to prompt the user to enter an integer.

    System.out.print("Please enter your integer ");
    y = Keyboard.in.readInteger().intValue();

    Thanks!

  9. #9
    Join Date
    Feb 2004
    Posts
    541
    The code you've used is likely to fail. The getInteger() method checks that the user has entered an integer for you. If they haven't, then it returns null.
    Code:
    y = Keyboard.in.readInteger().intValue();
    The above code will throw a null pointer exception if the user doesn't enter an integer, because the readInteger() method will return null. A better way to do it would be something like the following.
    Code:
    Integer theIntObject = null;
    int y = 0;
    
    while(theIntObject == null)
    {
        System.out.println("Enter an int between 0 and 9");
    
        theIntObject = Keyboard.in.readInt();
    
        if(theIntObject==null)
        {
            System.out.println("You didn't enter an integer");
        }
        else
        {
            int value = theIntObject.intValue();
            if(value < 0 || value > 9)
            {
                 theIntObject = null;
                 System.out.println("You didn't enter an int between 0 and 9);
            }
            else
            {
                y = value;
            }
        }
    }

  10. #10
    Join Date
    Oct 2004
    Posts
    18
    Thank you once again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center