Friday, February 8, 2013

Week-2: 2.6: Accepting Input from User

Accepting Input from User:

One of the strengths of Java is the huge libraries of code available to you. This is code that has
been written to do specific jobs. All you need to do is to reference which library you want to use,
and then call a method into action. One really useful class that handles input from a user is called
the Scanner class. The Scanner class can be found in the java.util library. To use the Scanner
class, you need to reference it in your code. This is done with the keyword import.

import java.util.Scanner;

The import statement needs to go just above the Class statement:
import java.util.Scanner;
public class StringVariables {

This tells java that you want to use a particular class in a particular library - the Scanner class,
which is located in the java.util library.
The next thing you need to do is to create an object from the Scanner class. (A class is just a
bunch of code. It doesn't do anything until you create a new object from it.)
To create a new Scanner object the code is this:

Scanner user_input = new Scanner( );

Visit for Details

No comments:

Post a Comment