Upload
bertina-foster
View
212
Download
0
Embed Size (px)
Citation preview
String and Scanner
CS 21a: Introduction to Computing I
First Semester, 2013-2014
The String Class►String: a built-in class in Java►Methods on String objects:
► public int length()► public String toUpperCase()► public String substring( int first, int last )
► javap java.lang.String for a complete list► Note: strings are immutable (no mutator
methods)►String objects have a special treatment in
Java► To enable string literals, string display, and
string concatenation
Using Strings
►String literal example: "Hello, World"►String variable:String message = "Hey";// same as String message = new String("Hey");
►Using strings:println( "Hello, World" );println( message );println( message.length() );String caps = message.toUpperCase();println( caps );
Prints:Hello,
WorldHey3HEY
Strings are Objects
►String variables contain object references
String s = "Hey";
►Calling length on a stringint x = s.length();
s
"Hey"
length()"Hey"3
Strings are Objects
►Calling toUpperCase on a string returns another stringString caps = s.toUpperCase();
►Note that the state of s does not change in this case
toUpperCase()
"Hey""HEY"
String Concatenation
►The + operator causes a concatenation if the operands are stringsprintln( "basket" + "ball" );
► If only one operand is a string, the other operand is first converted to a string and then a concatenation is performedint ans = 5;println( "The answer is " + ans );
Prints:basketbal
l
Prints:The answer is 5
Converting betweenStrings and Number Types
►Supposeint i = 5; double d = 10.0; String s = "7";
►From String to numberi = Integer.parseInt( s ); // assigns 7 to i
d = Double.parseDouble( s ); // assigns 7.0 to d
►From number to Strings = Integer.toString( i ); // assigns "5" to s
s = Double.toString( d ); // assigns "10.0" to s
► Can also use concatenations = "" + d;
The substring Method► Each character in a String object has a position
(starting with 0)► The parameters for substring indicate:
► first: the starting letter of the substring of interest► last: the position following the ending letter of the
substring► This way, (last-first) = the length of the resulting
substring► Example:String s = "Ateneo de Manila";String a = s.substring( 0, 6 ); // "Ateneo"String b = s.substring( 6, 9 ); // " de"String c = s.substring( 10,16 ); // "Manila"
Reading Console Inputand the Scanner Class
► To enable console input:► Before the declaration of the application class, type:
import java.util.Scanner;► At the beginning of the main method of the Java
application, create a scanner object:Scanner in = new Scanner( System.in );
► Then, invoke methods on the Scanner objectint i = in.nextInt();
double d = in.nextDouble();
String s = in.nextLine(); // reads an entire line of input
String w = in.next(); // reads one word only
Input Exampleimport java.util.Scanner;public class DollarToPesoConversion{ public static void main( String args[] ) { Scanner in = new Scanner( System.in ); System.out.print( "Type dollar amount: " ); double dollars = in.nextDouble(); System.out.print( "Conversion rate: " ); double rate = in.nextDouble(); System.out.print( "Pesos:" ); System.out.println( dollars*rate ); }}
Practice Programming Problem
►Write a program that reads one name from console and says hello to it.
►Sample InputJustin Bieber
►Sample OutputHello, Justin Bieber!