Java Input and Output for Beginners

This tutorial will teach you the basics of Java programming - more specifically, input and output. Furthermore, it will give you the opportunity to operate the open source Eclipse IDE (integrated development environment) and write one of your first programs in the Java language.

What You Will Need

  1. You will need to have the Java JRE installed on your computer. However, the Java JDK includes the JRE, so this will work as well. These are both available here:

    http://java.sun.com/javase/downloads/index.jsp

    Install the JRE/JDK on your computer (detailed installation instructions are available on the same web page).

  2. An IDE. I recommend the Eclipse IDE and will be using it for this tutorial. It is a powerful open source application that will help you write your programs, and it is great for debugging. You will need the Eclipse IDE for Java EE Developers. It is available here:
    http://www.eclipse.org/downloads/

Starting the Code

Since this is your first time using Eclipse, it will have a welcome screen offering to show you how to use Eclipse. It would be beneficial for you to test out Eclipse before you start, but if not, let's move on.

Select File > New > Java Project.

A plethora of options will pop up, but you only need one thing. In the Project Name text field, type "SampleIO", then click Finish.

Your "SampleIO" project will appear in the workspace directory in the left window of your Eclipse IDE. Right-click this "SampleIO" project and select New > Class from the drop-down menu. Another set of options will appear. At the Name text field, type "SampleIO", then click Finish (both names must be the same).

You probably noticed that a window appeared in the middle of the IDE with this bit of code:

public class SampleIO {

}

That is just Eclipse helping you start you code off. We are going to add to it.

So what is a class? To make things simple, a class is basically a collection of methods and variables. So we need to make some methods and variables to get this program running. A method is sort of like a function in other languages, and this is where we write our statements.

So let's write a method for our class. Our first method always needs to be the main() method, and by convention, follows a specific signature:

public static void main(String[] args)

The specific elements that make up methods will be discussed in another tutorial. This method signature is the standard main method, so to keep it simple, let's move on.

public class SampleIO {
       
        // Below is the main method.
        public static void main(String[] args) {
               
        }
}

Wait. What is this // thing I see? This is called a comment. Comments are not compiled by the IDE and are actually ignored, but they are useful for many things - such as making notes for other programmers, or blocking out code you don't wish the compiler to see.

There are two main ways to make comments (there is another for the Javadoc, but I will discuss that in a different tutorial). The first one is a single-line comment, which is preceded by the // delimeter. A multi-line comment is preceded by the /* delimeter and ends with the */ delimeter.

Anyways, now that we have created our method, let's add some statements to it. Let's write an output statement first, to let the user know what we want them to input.

public class SampleIO {
       
        // Below is the main method.
        public static void main(String[] args) {
               
                // Ask the user for some input.
                System.out.println("What is your first name?");
        }
}

Working With the Input Scanner

Well it looks like we want the user to input their name, so we are going to have to store that input into a variable. This variable will clearly need to be a String, and I am going to call it firstName. So this is our statement:

String firstName;

From here, we need to create a statement that will receive the input. We can do this by using Scanner. However, to use Scanner, we must import it into our program using the import statement. This basically tells the compiler where to find Scanner so we can use it in our program:

import java.util.Scanner;

Now we can create our Scanner statement, where s is the name of our Scanner:

Scanner s = new Scanner(System.in);

After we add this, we need to actually get the input from the user and store it. This is done with a simple statement:

firstName = s.next();

This statement commands the Scanner s to retrieve the next token of information and store it into our firstName variable

So let's see what the code looks like now:

import java.util.Scanner;


public class SampleIO {
       
        // Below is the main method.
        public static void main(String[] args) {
               
                // Variables
                String firstName;
               
                // Scanner
                Scanner s = new Scanner(System.in);
               
                // Prompt the user for some input
                System.out.println("What is your first name?");
                firstName = s.next();
               
               
        }
}

Alright, now that we have the code to store the first name, let's get the last name from the user. We will need to have a variable to store the last name, an output statement to ask the user for it, and a statement to retrieve and store the input. We've already done this stuff before, so here are our new statements:

String lastName;
System.out.println("What is your last name?");
lastName = s.next();

I'll put these in the code for you:

import java.util.Scanner;


public class SampleIO {
       
        // Below is the main method.
        public static void main(String[] args) {
               
                // Variables
                String firstName;
                String lastName;
               
                // Scanner
                Scanner s = new Scanner(System.in);
               
                // Prompt the user for some input
                System.out.println("What is your first name?");
                firstName = s.next();
               
                System.out.println("What is your last name?");
                lastName = s.next();
        }
}

Output with Concatenation

Well, this code would work if we ran it, but it wouldn't be much of a program without showing the user something useful. So let's do something useful. Why don't we add the first name and last names together to make a full name.

Lucky for us, Java has a concatenation technique that simply requires the + character. We could use our System.out.println but let me show you something. The println tells the program to break to a new line after it is done writing to the screen, but if you use print, it won't break to the next line. Since this is our last output, lets go ahead and use print:

System.out.print("Your full name is " + firstName + " " + lastName);

So our final code looks like this:

// Joshua Du Lac
// Inferno Development
import java.util.Scanner;


public class SampleIO {
       
        // Below is the main method.
        public static void main(String[] args) {
               
                // Variables
                String firstName;
                String lastName;
               
                // Scanner
                Scanner s = new Scanner(System.in);
               
                // Prompt the user for some input
                System.out.println("What is your first name?");
                firstName = s.next();
               
                System.out.println("What is your last name?");
                lastName = s.next();
               
                // Final Output
                System.out.print("Your full name is " + firstName + " " + lastName);
               
        }
}

Okay, now let's run it. In Eclipse, press Ctrl + F11 (or the green icon with the white arrow) and it will run in the console window, which is at the bottom of the IDE.

For further discussion, please visit our Programming Forum.

Jay's picture

Thank you for the easy to

Thank you for the easy to follow exp.

Colin's picture

Thank you for the tutorial.

Thank you for the tutorial. It was easy to follow and great to start with.

ondari's picture

coool

coool

————

o- dev co

Anonymous's picture

hey how can you do a for

hey how can you do a for loop
program and form an image.. and the image will appear in the middle...
for example a boat image using * to form the boat...
please help

Anonymous's picture

I love the codes :))) very

I love the codes :))) very helpful. Especially to the beginners like me .

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.