View RSS Feed

penguinCoder

Scanner Class Cheat Sheet

Rate this Entry
by , 10-18-2012 at 12:26 AM (2700 Views)
The Scanner class can be used for many things other then just getting input from the user; however this cheat sheet/tutorial is just going to be about getting input from the user.

Defining the Scanner Class
Java Code:
import java.util.Scanner;

public class ClassName{
  ...code...
}
It will tell the translator to look in that directory for the code defining the Scanner class.


Declaring the Scanner
If you are just going to use the Scanner class inside of the main method, it should be declared inside of the main method.
Java Code:
public class ClassName{
  public static void main(String[] args){
    Scanner keyboard;
    ...code...
If you are going to be using the Scanner class within multiple methods, inside of the same class, you should then define it as a static data member.
Java Code:
public class ClassName{
  public static Scanner keyboard;

  public static void main(String[] args){
    ...code...
These are variable declaration with
  • the data type as 'Scanner'
  • the variable name as 'keyboard'

This variable will contain a reference to an instance of a Scanner.


Creating an instance of Scanner
Java Code:
keyboard = new Scanner( System.in );
Most of the time an instance of Scanner is created at the same time it is declared.

The keyword 'new' dynamically allocates storage for an instance of the Scanner. It also initializes the new instance. The new Scanner is attached to the standard input stream from the keyboard ('System.in'). The Scanner can also read from ASCII text files. It also returns the address of the storage in memory, and assigns this address to the Scanner variable.


Calling Functions of the Scanner Class

For Integers
Java Code:
int count;
System.out.println("Enter count: ");
count = keyboard.nextInt();
For Floats
Java Code:
float cost;
System.out.println("Enter cost: ");
cost = keyboard.nextFloat();
For Doubles
Java Code:
double distance;
System.out.println("Enter distance: ");
distance = keyboard.nextDouble();
For Strings with NO Spaces
Java Code:
String lastName;
System.out.println("Enter last name: ");
lastName = keyboard.next();
For Strings WITH Spaces
Java Code:
String name;
System.out.println("Enter full name: ");
name = keyboard.nextLine();
For Characters
Java Code:
char response;
System.out.println("Again? (y/n)");
response = keyboard.next().charAt(0);
Another little trick of the Scanner class is when you are displaying more then a page of information, you might want to prompt the user before the information runs off the page.
Java Code:
System.out.println("Press Enter to Continue");
keyboard.nextLine();
keyboard.nextLine();

Submit "Scanner Class Cheat Sheet" to Facebook Submit "Scanner Class Cheat Sheet" to Digg Submit "Scanner Class Cheat Sheet" to del.icio.us Submit "Scanner Class Cheat Sheet" to StumbleUpon Submit "Scanner Class Cheat Sheet" to Google

Updated 10-18-2012 at 12:29 AM by penguinCoder

Categories
Cheat Sheet

Comments