Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Input and Output

You have already done output (System.out.println("text here")), so this small tutorial will simply expand on the HelloWorld application to ask for your name and print a less general hello message :-)

To do list:
1. import the java input output (io) libraries.
2. Learn how to ignore errors (ie: if user doesn't input the right thing)
3. Setup a keyboard reader (inputstreamreader)
4. Setup a buffer for the reader, after all you want a name not a letter in a name right? (buffered reader)
Lets get to it, create a new project named HelloWorld3, with a class named HelloWorld3 containing static main?
5. Read and store the data

Explanation with code:

1. Import java input io libraries.
Although java is known as big a bulky it does not load unnecessary libraries, you must tell it what you want, you do this by writing import and then the library name at the very top of the class (above the class declaration).
Add
Code: import java.io.*;
to the very top line.

2. Learn how to ignore errors.
Unlike VB Java prefers you to handle all errors, even if your user is Einstein and will never enter anything incorrect in your application, you must still handle for that event! Right now im just gonna show you how to throw your error outside of the main class. (this ignores the error and exits)
Code: public static void main(String[] args) throws IOException{
The main function now throws an input output, error if something goes wrong, which basicly meens it exits on an error.
3. Setup a keyboard reader
In the main function tell java to setup a keyboard reader (inputstreamreader) and call it "isr".
Code: InputStreamReader isr = new InputStreamReader( System.in );
4. Setup a buffer for the reader
Underneith that tell java to setup a buffer (storage) using the keyboard reader, to wait until for example the user hits enter and give a whole word.
Code: BufferedReader stdin = new BufferedReader( isr );
5. Read and store the data
Now everything is setup reading is simple, simply write a message saying what you want the user to do, and set your variable to equal the input buffer dot readLine. ReadLine simply means java waits until the user hits return before giving a result.
Code:
System.out.println("Enter your title: (Mr/Mrs/Dr)");
strTitle=stdin.readLine();
System.out.println("Enter your forename:");
strForeName=stdin.readLine();
System.out.println("Enter your surname:");
strSurName=stdin.readLine();
Final code:
Code:
import java.io.*;
public class HelloWorld3 {

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

/* Expanding the HelloWorld Sample */

// Original Hello World Using a variable
String strHelloWorld="Hello World";
System.out.println(strHelloWorld);

// Joining multiple names and displaying
// try replacing with your names!
String strTitle = "";
String strForeName = "";
String strSurName = "";
String strFullName = ""; //<-- set this automaticaly

/* NEW STUFF */
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader( isr );

System.out.println("Enter your title: (Mr/Mrs/Dr)");
strTitle=stdin.readLine();
System.out.println("Enter your forename:");
strForeName=stdin.readLine();
System.out.println("Enter your surname:");
strSurName=stdin.readLine();
/* OLD STUFF */

// join names
strFullName = strTitle+" "+strForeName+" "+strSurName;
/* The '+' symbol joins strings and adds numbers! To join a number use ""+ more later */
// show result
System.out.println("Hello: "+strFullName);
}
}
Screenshot

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Guest says:17-Sep-2008 05:21am



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source