File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes outside inputs (keyboard) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "outside inputs (keyboard)" Watch "outside inputs (keyboard)" New topic

outside inputs (keyboard)

Tim Hoang

Joined: Feb 24, 2011
Posts: 26
Is this a beginner lesson, on keyboard inputs? ( the key pressed will output a code, e.g. press letter "A" would output number 1 if coded to output 1.)

Also, how do I use operators to create a value to be used for another object without printing out the value yet? e.g [ X = (3-2 = ??) ] then X would be used later on.

hope this is clear. I hope to make sort of a calculator project with keyboard.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I'm confused by your question. Are you asking how to determine if a user inputs the letter "A" from the keyboard?

As to your calculator question, you would have to read the value "X = 3 -2" as a string then search through that string to determine 1) what numbers are being used and 2) what operation is being performed, then 3) store the result of that operation in some sort of structure(i.e. an array or list).

Hope this helps,

"If the facts don't fit the theory, get new facts" --Albert Einstein
Tim Hoang

Joined: Feb 24, 2011
Posts: 26
I want to make a keyboard in use. So is there a way to make it interact with the package? Like in order to make a program activate (password), you must type in your keyboard certain values for it to work right. What I wanted to do was use this to create a calculator with a keyboard.

Step 1. Program says (e.g) : put in values
Step 2. I type (keyboard) values: (3) - (2)
Step 3. Program works then outputs: (1)

Will this be a very difficult task for a beginner of 2 months of lessons?

was that clear? Thank You for responding

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

If you want to read keyboard input from the console, first you'll have to get hold of the console's input stream, which you can get by (same to how you'd use System.out to get the output stream of the console), and then you can call read() method on it to read from the screen.
Alternatively you can wrap this InputStream in a BufferedReader via InputStreamReader or in a Scanner for reading directly as String, rather than reading into a byte array.
You can find many examples over the net.

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I don't think the actual program will be difficult, the hard part will be understanding how to accomplish this. Whatever you enter from the keyboard will be considered text and thus String input.

So a general way to accomplish this would be:
1) find a way to SCAN the user's input and store it inside a String variable.
2) Use operations in the String class to extract the information you need from the String. (i.e. the numbers and the operator)
3) Output the value of the calculation

I'm not going to go into coding detail, because that is up to you good luck.

hope this helps,
Tim Hoang

Joined: Feb 24, 2011
Posts: 26
Does anyone have any tips on accomplishing the keyboard class? Package? Is there an import on keyboard I could learn about in the JDK . At this point I don't even know what I am saying,but it should be easy once I find info on keyboard.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

At this point I don't even know what I am saying,but it should be easy once I find info on keyboard.

Obviously, you don't seem to know what you're saying But everyone has to start. However, I don't think that poking around like that will help you. I'd like to suggest you to get your hands on a good book and start from the start.

If you insist on getting straight to the keyboard thing, check this tutorial on IO. Especially "Scanning" and "I/O from the command line". The Scanner class API also has some examples on how to get data from the standard input.

[My Blog]
All roads lead to JavaRanch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37890
And avoid the read() method. It is awkward to use.
I agree. Here's the link:
subject: outside inputs (keyboard)
Similar Threads
Is this java's Bug
loop iterator
Another switch statement query
Element values in different rows