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

Basics of Java Programming Input and The Scanner Class: CSC 1051 - Algorithms and Data Structures I

This document discusses using the Scanner class in Java to take user input. It begins with an overview of variables and assignment statements. It then introduces the Scanner class for obtaining input during program execution. It explains how to import the Scanner class, create a Scanner object, and use various next methods like nextInt() and nextLine() to retrieve input of different data types. Examples are provided to demonstrate taking name and age input to print a personalized message. The document aims to teach the basics of interactive input/output in Java programs.

Uploaded by

DorivenBasha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Basics of Java Programming Input and The Scanner Class: CSC 1051 - Algorithms and Data Structures I

This document discusses using the Scanner class in Java to take user input. It begins with an overview of variables and assignment statements. It then introduces the Scanner class for obtaining input during program execution. It explains how to import the Scanner class, create a Scanner object, and use various next methods like nextInt() and nextLine() to retrieve input of different data types. Examples are provided to demonstrate taking name and age input to print a personalized message. The document aims to teach the basics of interactive input/output in Java programs.

Uploaded by

DorivenBasha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Basics of Java Programming

Input and the Scanner class


CSC 1051 Algorithms and Data Structures I
Dr. Mary-Angela Papalaskari
Department of Computing Sciences
Villanova University

Course website:
www.csc.villanova.edu/~map/1051/
Some slides in this presentation are adapted from the slides accompanying:

Java Software Solutions by Lewis & Loftus



Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne

CSC 1051 M.A. Papalaskari, Villanova University

Review

Variables
A variable is a name for a location in memory, of a declared type
An assignment statement associates a value with a variable
A variable can be given an initial value in the declaration

int age;
double x = 3.2, y = -0.80;
"
age = 18;"
x = 3.3;"
String name = scan.nextLine();"
CSC 1051 M.A. Papalaskari, Villanova University

Variables
A variable is a name for a location in memory, of a declared type
An assignment statement associates a value with a variable
A variable can be given an initial value in the declaration

Next: assigning a value obtained while the program is running

int age;
double x = 3.2, y = -0.80;
"
age = 18;"
x = 3.3;"
String name = scan.nextLine();"
CSC 1051 M.A. Papalaskari, Villanova University

Interactive Programs Input/Output


Programs can use data obtained during runtime, eg:
int age;
"
String name;"
"
Scanner scan = new Scanner(System.in);!
output method
"
System.out.print(Enter your name);"
name = scan.nextLine();"
input method
"
System.out.print(Enter your age); "
age = scan.nextInt();"
);"
CSC 1051 M.A. Papalaskari, Villanova University

Interactive Programs Input/Output


In Java, you first need to create a Scanner object
int age;
"
String name;"
Scanner object
"
Scanner scan = new Scanner(System.in);!
"
System.out.print(Enter your name);"
name = scan.nextLine();"
input method (for String)
"
System.out.print(Enter your age); "
age = scan.nextInt();"
"
input method (for int)
CSC 1051 M.A. Papalaskari, Villanova University

Reading Input
The Scanner class is part of the java.util class
library, and must be imported into a program in
order to be used
The import statement goes at beginning of your
program (above class definition)

import java.util.Scanner;

CSC 1051 M.A. Papalaskari, Villanova University

Using the Scanner class


1. import the class, i.e., add this before the class definition of
your program:
import java.util.Scanner;"
2. In your main method, before doing any input, declare and
initialize the Scanner object
Scanner scan = new Scanner(System.in);"
3. Input away!
System.out.print(Enter your name);"
name = scan.nextLine();"
"
System.out.print(Enter your age); "
age = scan.nextInt();String "
CSC 1051 M.A. Papalaskari, Villanova University

Using the Scanner class

import java.util.Scanner;"
"
public class TellMeAboutYou "
{ "
public static void main(String[] args) "
Enter your name: Fiona
{"
Enter your age: 17
int age;
"
Pleased to meet you, Fiona!
String name; "
Your age in dog years is 178.5
"
Scanner scan = new Scanner(System.in); "
"
System.out.print("Enter your name"); "
name = scan.nextLine(); "
"
System.out.print("Enter your age"); "
age = scan.nextInt(); "
"
System.out.println("Pleased to meet you, " + name + "!");"
System.out.println("Your age in dog years: " + age*10.5);"
}"
} name = scan.nextLine();" Inspired by: http://www.onlineconversion.com/dogyears.htm
CSC 1051 M.A. Papalaskari, Villanova University

Input methods
nextInt() input an int
nextDouble() input a double
nextLine() input a String (until end of line)
next() input a String token (one word or
other delimited chunk of text)
White space (space, tab, new line) are used to
separate input tokens

CSC 1051 M.A. Papalaskari, Villanova University

Using the Scanner class

import java.util.Scanner;"
"
public class TellMeAboutYou "
{ "
public static void main(String[] args) "
{"
Enter your name and age: Fiona 17
int age;
"
Pleased to meet you, Fiona!
String name; "
Your age in dog years is 178.5
"
Scanner scan = new Scanner(System.in); " fill in missing code
"
System.out.print("Enter your name and age:
);
name = scan.nextLine(); "
age = scan.nextInt(); "
"
"
System.out.println("Pleased to meet you, " + name + "!");"
System.out.println("Your age in dog years: " + age*10.5);
}"
} name = scan.nextLine();"
More examples see text: Echo.java
GasMileage.java
CSC 1051 M.A. Papalaskari, Villanova University

You might also like