Download C248-ch2-ConsoleInput

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP 248: OBJECT ORIENTED
PROGRAMMING I
Chap 2 - Console Input Part only
1
CLASS LIBRARIES & PACKAGES

A class library (ex: Java standard class library) is a
collection of classes that we can use when developing
programs

The classes of the Java standard class library are organized
into packages
Package
Purpose
java.lang
java.text
java.applet
java.awt
java.util
General support
Format text for output
Creating applets for the web
Graphics and graphical user interfaces
Utilities
2
THE IMPORT DECLARATION
to use a class from a package


you can import only the class DecimalFormat from the
package java.text
import java.text.DecimalFormat;

or import all classes from the package java.text
import java.text.*;

All classes of the java.lang package are imported
automatically into all programs

That's why we didn't have to import the System or
String classes explicitly
3
CONSOLE INPUT

since Java 5.0, use the Scanner class

the keyboard is represented by the System.in object
1.
2.
import java.util.Scanner;
Scanner myKeyboard = new Scanner(System.in);
4
TO READ FROM A SCANNER

to read tokens, use a nextSomething() method








nextBoolean(),
nextByte,
nextInt(),
nextFloat(),
nextDouble(),
next(),
nextLine()
…
tokens are delimited by
whitespaces (blank spaces,
tabs, and line breaks)
Scanner myKeyboard = new Scanner(System.in);
System.out.println("Your name:");
String name = myKeyboard.next();
System.out.println("Welcome " + name + " Enter your age:");
int age = myKeyboard.nextInt();
5
EXAMPLE 1: SCANNERDEMO.JAVA
//*****************************************************
// Author: W. Savitch (modified by L. Kosseim)
//
// This program demonstrates how to read tokens from
// the console with the Scanner class
//*****************************************************
import java.util.Scanner; // we need to import this class
public class ScannerDemo
{
public static void main(String[] args)
{
// let's declare our scanner
Scanner keyboard = new Scanner(System.in);
6
EXAMPLE 1: SCANNERDEMO.JAVA
// let's ask the user for some input
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod:");
// let's read the user input (2 integers that we assign to 2
// variables)
int numberOfPods = keyboard.nextInt( );
int peasPerPod = keyboard. nextInt( );
int totalNumberOfPeas = numberOfPods*peasPerPod;
7
EXAMPLE 1: SCANNERDEMO.JAVA
// let's display some output
System.out.print(numberOfPods + " pods and ");
System.out.println(peasPerPod + " peas per pod.");
System.out.println("The total number of peas = "
+ totalNumberOfPeas);
}
}
8
EXAMPLE 2: SCANNERDEMO2.JAVA
//****************************************************************
// Author: W. Savitch (modified by L. Kosseim)
//
// This program demonstrates how to read various types of token with
// the Scanner class
//****************************************************************
import java.util.Scanner;
public class ScannerDemo2
{
public static void main(String[] args)
{
// let's try to read integers
int n1, n2; // let's declare 2 variables for our tests
// let's declare our scanner object
Scanner scannerObject = new Scanner(System.in);
9
EXAMPLE 2: SCANNERDEMO2.JAVA
System.out.println("Enter two whole numbers");
System.out.println("separated by one or more spaces:");
// we read 1 integer and assign it to n1
n1 = scannerObject.nextInt( );
// we read another integer and assign it to n2
n2 = scannerObject.nextInt( );
System.out.println("You entered " + n1 + " and " + n2);
System.out.println("Next enter two numbers.");
System.out.println("Decimal points are allowed.");
10
EXAMPLE 2: SCANNERDEMO2.JAVA
System.out.println("Next enter two numbers.");
System.out.println("Decimal points are allowed.");
// let's try to read doubles now
double d1, d2;
d1 = scannerObject.nextDouble( );
d2 = scannerObject. nextDouble( );
System.out.println("You entered " + d1 + " and " + d2);
11
EXAMPLE 2: SCANNERDEMO2.JAVA
// let's try to read 2 "words" now
System.out.println("Next enter two words:");
String word1 = scannerObject.next( );
String word2 = scannerObject.next( );
System.out.println("You entered \"" +
word1 + "\" and \"" + word2 + "\"");
//To get rid of '\n'
String junk = scannerObject.nextLine( );
12
EXAMPLE 2: SCANNERDEMO2.JAVA
// let's try to read an entire line
System.out.println("Next enter a line of text:");
String line = scannerObject.nextLine( );
System.out.println("You entered: \"" + line + "\"");
}
}
13
A NOTE ON READLINE



nextLine reads the remainder of a line of text starting where the last
reading left off
This can cause problems when combining it with different methods for
reading from the keyboard such as nextInt
ex:
Scanner keyboard = new Scanner(System.in);
int n = keyboard.nextInt();
String s1 = keyboard.nextLine();
String s2 = keyboard.nextLine();
input:
2
Heads are better than
1 head.
what are the values of n, s1, and s2?
need an extra invocation
of nextLine to get rid of
the end of line character
after the 2
14