|
Graeme Kerry
is a first year computer systems engineering student
from the UK, studying at Lancaster University. He has been
programming in Java for about 3 years. Outside of studies
he enjoys sports and socialising with friends.
Kezz's Java Tutorials are written in Graeme's
spare time.
|
Part 2 - Getting User Input
Welcome to the second of my Java tutorials. In this tutorial I am going to go
over how to obtain the user's input. As you may realise this is quite an
important topic as if you didn't know how to do this, your programs would be
very boring!
Fire up your text editor of choice and enter the following program:
import java.io.*;
public class UserInput {
public static void main(String [] args)
throws IOException {
BufferedReader in = new
BufferedReader(new InputStreamReader(System.in)); String name = "";
System.out.println("Enter your name:
"); System.out.flush(); name =
in.readLine(); System.out.println("Thank you. " +
name);
}
}
This code looks more like it! Now remember to save the file as UserInput.java
(nothing else will do!!) and compile it using javac. Now run it. If you get any
unexpected results check over the example code line by line and see where the
error is.
Now lets dissect the code:
import java.io.*;
This is the first time you have met an import statement in your Java travels.
Basically what import allows you to do is include all kinds of useful functions
and methods provided by other people (or yourself). These are stored in other
java programs or libraries. In this case you are importing the Java Input Output
library. This library is provided with the Java SDK and will be installed.
public class UserInput {
public static void main(String [] args)
throws IOExeption {
You already know what most of this code does apart from the IOException at
the end. What this does is tell the Java system that something could go wrong in
regards to I/O and to ready itself for such a situation. The main thing to
remember is if using java.io.* is to ensure you include a throws IOException in
your code.
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
This is an important part of the program.It creates
a new BufferedReader object called in and tells it to read data from the
standard input, in this case (and most others) the keyboard. Again don't worry
too much about the syntax just remember how to use it at this stage.
String name =
"";
This is another key piece of the program and also a
key piece of java. This line creates a variable, a String variable to be more
precise. A variable is a place in your computers memory where you can store
surprisingly enough variable information, or in other words information that can
be changed. A String variable therefore can hold a String. A String is a line of
text for example "the cat sat on the mat" is an example of a String variable. In
this case it is called name and is set to initially hold nothing (or "").
System.out.println("Enter your name:
"); System.out.flush();
The first line shouldn't be a problem. The second
empties the buffer so that no garbage is left in it to give you unexpected
results.
name =
in.readLine();
This line is probably the MOST important in the
program. Basically it reads from the keyboard using the readLine() method and
stores the result in name (the String variable we created and set to ""). Notice
how the readLine function is called. It belongs to the BufferedReader class of
which 'in' is an instance we created above.
System.out.println("Thank you. " + name);
This line prints the results of the program to the
screen, notice how the statement prints the contents of the name variable not
"name" itself. If I had wanted to print "name" to the screen I would have put it
between speechmarks. Therefore no speechmarks means 'content of' if you
like.
Finishing
off
I'm afraid thats all for this time. Next time I am
going to cover variable types (might be useful) and commenting. If in the
meantime you have any problems you can contact me at my email address and I will try to
answer your questions.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|