Transcript Strings

Strings and I/O
Lotsa String Stuff…
There are close to 50 methods defined in the String
class. We will introduce several of them here: charAt,
substring, length, and indexOf.
We will also introduce a string operation called
concatenation.
String Indexing
The position, or index, of
the first character is 0.
charAt(i) tells the character at position i
char a = text.charAt(0);
‘E’
char b = text.charAt(4);
‘e’
char c = text.charAt(7);
‘o’
char d = text.charAt(8);
error
Definition: substring
Assume str is a String object and properly initialized to
a string, then str.substring( i, j ) will return a new string
by extracting characters of str from position i to j-1.
String word = text.substring(1,4);
word is “spr”
Examples: substring
String a = text.substring(6,8);
“so”
String b = text.substring(0,8);
“Espresso”
String c = text.substring(1,5);
“spre”
String d = text.substring(3,3);
“”
String e = text.substring(4,2);
error
Definition: length
Assume str is a String object and properly initialized to
a string.
str.length( ) will return the number of characters in str.
If str is “programming” , then str.length( ) will return 11
because there are 11 characters in it.
The original string str remains unchanged.
Examples: length
String
str1 =
str2 =
str3 =
str4 =
str1, str2, str3, str4;
“Hello” ;
“Java” ;
“” ; //empty string
“ “ ; //one space
int a = str1.length( );
5
int b = str2.length( );
4
int c = str3.length( );
0
int d = str4.length( );
1
Definition: indexOf
Assume str and substr are String objects and properly
initialized.
str.indexOf( substr ) will return the first position substr
occurs in str.
If str is “programming” and substr is “gram” , then
str.indexOf(substr ) will return 3 because the position of
the first character of substr in str is 3.
If substr does not occur in str, then –1 is returned.
The search is case-sensitive.
Examples: indexOf
String str;
str = “I Love Java and Java loves me.” ;
3
7
21
int a = str.indexOf( “J” );
7
int b = str.indexOf( “love” );
21
int c = str. indexOf( “ove” );
3
int d = str. indexOf( “Me” );
-1
Examples: indexOf
String str;
str = “I Love Java and Java loves me.” ;
int e = str.indexOf( “J”,8 );
16
int f = str.lastIndexOf(“J”);
16
Definition: concatenation
Assume str1 and str2 are String objects and properly
initialized.
str1 + str2 will return a new string that is a
concatenation of two strings.
If str1 is “pro” and str2 is “gram” , then str1 + str2 will
return “program”.
Notice that this is an operator and not a method of the
String class.
The strings str1 and str2 remains the same.
Examples: concatenation
String str1, str2;
str1 = “Jon” ;
str2 = “Java” ;
String a = str1 + str2;
“JonJava”
String b = str1 + “ “ + str2;
“Jon Java”
String c = str2 + “, “ + str1;
“Java, Jon”
String d = “Are you “+str1+“?”;
“Are you Jon?”
What do you think it does?
String str = “Hey there”;
String str1 = str.toLowerCase(); //”hey there”
int ind = str.indexOf(“e”,4);
// 6
int ind = str.lastIndexOf(“e”);
// 8
String str2 = str.substring(4);
// “there”
String Equality
String a = “cat”;
String b = “cat”;
boolean c = (a == b);
Is c true or false?
String Equality
String a = “cat”;
String b = “cat”;
boolean c = (a == b);
Is c true or false?
a -> c a t
b -> c a t
a and b are not the same
object, so the answer is
false
String Equality
String a = “cat”;
String b = a;
boolean c = (a == b);
Is c true or false?
a -> c a t
b
a and b are the same
object, so the answer is
true, but usually this isn’t
what we have.
String Equality
String a = “cat”;
String b = “cat”;
boolean c = a.equals(b);
Use the “equals” behavior, which means the two
strings are equivalent.
The value of c is true.
Input and Output
• Two ways to input and output values
•Console input and output (like System.out)
•JOptionPanes (a.k.a. pop-up windows)
Standard Output Window
A sample standard output window for displaying multiple
lines of text.
• The exact style of standard output window
depends on the Java tool you use.
The print Method
We use the print method to output a value to the standard output
window.
The print method will continue printing from the end of the
currently displayed output.
Example
System.out.print( “Hello, Dr. Caffeine.” );
The println Method
We use println instead of print to skip a line.
int x = 123, y = x + x;
System.out.println( "Hello, Dr. Caffeine.“ );
System.out.print( " x = “ );
System.out.println( x );
System.out.print( " x + x = “ );
System.out.println( y );
System.out.println( " THE END“ );
Standard Input
The technique of using System.in to input data is
called standard input.
To input primitive data values, we use the Scanner
class (from Java 5.0).
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
Need to:
import java.util.Scanner;
Common Scanner Methods:
Method
nextDouble( )
nextInt( )
next()
nextLine()
Example
double d =scanner.nextDouble( );
int i = scanner.nextInt( );
String str = scanner.next();
String wholeLine = scanner.nextLine();
Static Objects
Some objects can be used without being created via
“new.”
Use them directly:
<ClassName>.<methodName>();
JOptionPane
A static object (don’t use new)
Must import javax.swing.JOptionPane to use.
Using showMessageDialog of the JOptionPane class is a simple way to
display a result of a computation to the user.
JOptionPane.showMessageDialog(null, “I Love Java”);
This dialog will appear
at the center of the
screen.
Displaying Multiple Lines of Text
We can display multiple lines of text by separating lines with a
new line marker \n.
JOptionPane.showMessageDialog(null,
“one\ntwo\nthree” );
JOptionPane for Input
Using showInputDialog of the JOptionPane class is a simple
way to input a string.
String name;
name = JOptionPane.showInputDialog
(null, “What is your name?”);
This dialog will appear
at the center of the
screen ready to accept
an input.
Type Mismatch
Suppose we want to input an age. Will this work?
int
age;
age = JOptionPane.showInputDialog(
null, “Enter your age”);
• No.
String value cannot be assigned directly to an
int variable. It can’t even be cast into an int.
Type Conversion
Wrapper classes are used to perform necessary
type conversions, such as converting a String object
to a numerical value.
String inputStr = JOptionPane.showInputDialog(
null, “Enter your height”);
double height = Double.parseDouble(inputStr);
Type Conversion
Works for ints too…
String inputStr = JOptionPane.showInputDialog(
null, “Enter your age”);
int age = Integer.parseInt(inputStr);
How do you remember it all?
You don’t!
API: Application programming interface
Check it out...