Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java user input and output

So far, we have focused only on one sided Java concepts - that is, no user input is accepted. Accepting user input is a great way to allow users to interact with a program.

This lesson focuses on:

Importing the necessary package for user input

The package that needs to be imported to accept user input is java.io. The java.io package contains classes and interfaces used for input and output.

Example:

import java.io.*;

class GetUserInput{

}

Using the necessary classes for user input

To get user input, use the BufferedReader and InputStreamReader classes.

Example:

class GetUserInput{

    public static void main(String[] args){

        //the data that will be entered by the user
	String name; 

        //an instance of the BufferedReader class
        //will be used to read the data
	BufferedReader reader;

        //specify the reader variable 
        //to be a standard input buffer
	reader = new BufferedReader(new InputStreamReader
                                    (System.in));

        //ask the user for their name
	System.out.println("What is your name?");

        //read the data entered by the user using 
        //the readLine() method of the BufferedReader class
        //and store the value in the name variable
        name = reader.readLine();

    }

}

Handling the exceptions

Using the above code, you still cannot accept user input because one thing is still missing, and that is you have to catch the exception(s).

What if the code does not work properly? What if the user does not enter any data? Such situations are called exceptions, and in programs that accept user input, you need a way to handle them. The exception we will be using is the IOException. This exception is used when an input error occurs. Exceptions are coverted in detail in the next lesson. But for now, you should know that exceptions are handled with something called a try-catch block which is basically a block of code that will execute one set of code if there is no error and another set of code if there is an error.

Example:

class GetUserInput{

    public static void main(String[] args){

        //the data that will be entered by the user
	String name; 

        //an instance of the BufferedReader class
        //will be used to read the data
	BufferedReader reader; 

        //specify the reader variable
        //to be a standard input buffer
	reader = new BufferedReader(new InputStreamReader
                                   (System.in));

        //ask the user for their name
	System.out.println("What is your name?");

        try{

        //read the data entered by the user using 
        //the readLine() method of the BufferedReader class
        //and store the value in the name variable
        name = reader.readLine(); 

        //print the data entered by the user
       	System.out.println("Your name is " + name);
        
        }

        catch (IOException ioe){

            //statement to execute 
            //if an input/output error occurs
       	    System.out.println("An unexpected 
                                error occured.");
        
        }

    }

}

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments