advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Making the Switch to Java :

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.

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs