![](smile.gif)
[java] Java Input
Hi there,
I''m trying to create a little Java program that runs in a DOS command window etc etc etc, the thing I''m not sure about is getting input from the user.
I''ve always either worked with files or some sort of GUI when working in Java. What I would like to know is this: when I try and read from System.in.read() will I be able to get input from the keyboard?
Basically I want a little Menu set up like:
"Pick 1 or 2: " - the user then hits either "1" or "2" and then "" and I want to have an int set to whatever number the user typed in.
Haven''t really worked with Java and user input like this before so I basically want to know:
1) is this possible?
2) Will I be able to do this as simply as int i = System.in.read() ?
3) If not, what else do I need to do?
I''ve been messing with this a little and it doesn''t generae an error, but it also doesn''t seem to be waiting for my input..
Thanks a lot for your help, I really need it!!
![](smile.gif)
- Flanders -
Hello Flanders,
As you might expect there are many ways to read input from a user in Java depending on the format that best suits you.
Yes, you could use a System.in.read to reading user input.
java.io.InputStream (from System.in) supports three read methods:-
1) int read ()
2) int read (byte[] b)
3) int read (byte[] b, int off, int len)
I''m guessing you''ve been playing around with method 1), this just returns the next key as an int, these come out as ASCII values? ( I think )
The following code snippet, loops forever waiting for key presses, type ''a'' return and you''ll see the following output
97 ( ''a'' )
13 ( ''cariage return'' )
10 ( ''new line'' )
while (true)
{
int i = System.in.read();
System.out.println(i);
}
Using methods 2) or 3) you pass in an array which the method fills with bytes from the input stream, returning the number of bytes written.
Personally for what your doing I''d use one of the BufferedReader class on top of the basic InputStream. As this will allow you to process user input one line at a time.
BufferedReader l_InputSrc = new BufferedReader(new InputStreamReader(System.in));
String l_NextLine = l_InputSrc.readLine();
Tom.
As you might expect there are many ways to read input from a user in Java depending on the format that best suits you.
Yes, you could use a System.in.read to reading user input.
java.io.InputStream (from System.in) supports three read methods:-
1) int read ()
2) int read (byte[] b)
3) int read (byte[] b, int off, int len)
I''m guessing you''ve been playing around with method 1), this just returns the next key as an int, these come out as ASCII values? ( I think )
The following code snippet, loops forever waiting for key presses, type ''a'' return and you''ll see the following output
97 ( ''a'' )
13 ( ''cariage return'' )
10 ( ''new line'' )
while (true)
{
int i = System.in.read();
System.out.println(i);
}
Using methods 2) or 3) you pass in an array which the method fills with bytes from the input stream, returning the number of bytes written.
Personally for what your doing I''d use one of the BufferedReader class on top of the basic InputStream. As this will allow you to process user input one line at a time.
BufferedReader l_InputSrc = new BufferedReader(new InputStreamReader(System.in));
String l_NextLine = l_InputSrc.readLine();
Tom.
BUffered reader sounds like the best route to go for sure. Thank you for your help...
One additional question on top of this then..
BufferedReader is included in java.io.* ?
Very important to have the correct files imported
One additional question on top of this then..
BufferedReader is included in java.io.* ?
Very important to have the correct files imported
![](smile.gif)
- Flanders -
Hello Flanders,
I''m sure you''ve figured it out by now, but yes both BufferedReader and InputStreamReader are from the java.io package.
A useful reference site I use from time to time is the sun java site, covering jdk 1.3.
http://java.sun.com/j2se/1.3/docs/api/index.html
Tom
I''m sure you''ve figured it out by now, but yes both BufferedReader and InputStreamReader are from the java.io package.
A useful reference site I use from time to time is the sun java site, covering jdk 1.3.
http://java.sun.com/j2se/1.3/docs/api/index.html
Tom
Just to reiterate something Tom said, going to Sun''s site is extremely helpful. They have a lot of good libraries that are really well documented. I personally use SDK 1.2 (as opposed to 1.3, because 1.2 is supported by my school''s servers). Check out a page a little higher up in the hierarchy:
http://java.sun.com/docs/?frontpage-javaplatform
This will springboard you to any version''s API, along with some quick tutorials and some other useful Java info.
Hope that helps in the future.
OctDev
http://java.sun.com/docs/?frontpage-javaplatform
This will springboard you to any version''s API, along with some quick tutorials and some other useful Java info.
Hope that helps in the future.
OctDev
The Tyr project is here.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement