0% found this document useful (0 votes)
18 views

Scanner Notes

The document discusses the Scanner class in Java, which allows a program to read input from the user. It notes that the Scanner class parses input into tokens separated by whitespace by default. It demonstrates how to import the Scanner class, create a Scanner object, use methods like nextInt() and nextLine() to read different data types from the user, and addresses a warning about reading numeric input before string input.

Uploaded by

JOB APPLY HELP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Scanner Notes

The document discusses the Scanner class in Java, which allows a program to read input from the user. It notes that the Scanner class parses input into tokens separated by whitespace by default. It demonstrates how to import the Scanner class, create a Scanner object, use methods like nextInt() and nextLine() to read different data types from the user, and addresses a warning about reading numeric input before string input.

Uploaded by

JOB APPLY HELP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SCANNER NOTES

Mrs. Tillman
Computer Science
Using the Scanner Class
• Provides methods for reading byte, short, int, long, float,
double, and String data types from the user.
• Scanner is in the java.util package
• Scanner parses (separates) input into sequences of
characters called tokens.
• By default, tokens are separated by standard white space
characters (tab, space, newline, etc.)
Import Statement
You have to import the statement at the top in order to use
Scanner.
import java.util.Scanner;

public class TestTerminalIO


{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);

System.out.print ("Enter your name (a string): ");


String name = scan.nextLine();

System.out.print("Enter your age (an integer): ");


int age = scan.nextInt();

System.out.print("Enter your weight (a double): ");


double weight = scan.nextDouble();
Creating a New Scanner Object
You have to create a new Scanner Object in your code to use the
Scanner.
import java.util.Scanner;

public class TestTerminalIO


{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);

System.out.print ("Enter your name (a string): ");


String name = scan.nextLine();

System.out.print("Enter your age (an integer): ");


int age = scan.nextInt();

System.out.print("Enter your weight (a double): ");


double weight = scan.nextDouble();
Asking the User for Input
You use a System.out.prinln() to ask the user for input.

import java.util.Scanner;

public class TestTerminalIO


{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);

System.out.print ("Enter your name (a string): ");


String name = scan.nextLine();

System.out.print("Enter your age (an integer): ");


int age = scan.nextInt();

System.out.print("Enter your weight (a double): ");


double weight = scan.nextDouble();
Scanner Class
Scanner class: reads from the input stream

Here are the methods in the Scanner class:

METHOD DESCRIPTION
double nextDouble() Returns the first double in the input line.
Leading and trailing spaces are ignored.

int nextInt() Returns the first integer in the input line.


Leading and trailing spaces are ignored.

String nextLine() Returns the input line, including leading and


trailing spaces. Warning: A leading newline is
returned as an empty string.
Save and Cast the Data
You must use the methods for the Scanner class to cast the data
correctly.
import java.util.Scanner;

public class TestTerminalIO


{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);

System.out.print ("Enter your name (a string): ");


String name = scan.nextLine();

System.out.print("Enter your age (an integer): ");


int age = scan.nextInt();

System.out.print("Enter your weight (a double): ");


double weight = scan.nextDouble();
Scanner Class Output
You will use the System.out.println() to create output for program.

System.out.println ("Greetings " + name +


". You are " + age + " years old and you weigh " +
weight + " pounds.");
}
}
Quick Demo
Lets look at the Scanner Code….
Scanner Class WARNING!
Warning: If you accept numeric input BEFORE string input,
the string is ignored unless you take special action!!!!
// now change the order of the input,
// putting the string last

System.out.print("Enter your age (an integer): ");


int age = scan.nextInt();

System.out.print("Enter your weight (a double): ");


double weight = scan.nextDouble();

System.out.print ("Enter your name (a string): ");


String name = scan.nextLine();
Quick Demo
Lets look at this code…..
Scanner Class
The string name is the empty string. The reason is
that the method nextDouble() ignored but did not
consume the new line that the user entered following
the number. Therefore, this newline character was
waiting to be consumed by the next call of nextLine,
which was expecting more data.
To avoid this problem, you should either input all lines
of text before the numbers or, when that is not
feasible, run an extra nextLine() after numeric input to
eliminate the trailing newline characters.
Scanner Class WARNING!
Warning: If you accept numeric input BEFORE string input,
the string is ignored unless you take special action!!!!

// now change the order of the input,


// putting the string last

System.out.print("Enter your age (an integer): ");


int age = scan.nextInt();

System.out.print("Enter your weight (a double): ");


double weight = scan.nextDouble();

scan.nextLine(); // to consume the newline char


System.out.print ("Enter your name (a string): ");
String name = scan.nextLine();
Quick Demo
Lets see if this will fix it! 

You might also like