Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We...

32
Strings and I/O

description

String Indexing The position, or index, of the first character is 0.

Transcript of Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We...

Page 1: 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,

Strings and I/O

Page 2: 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,

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.

Page 3: 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,

String Indexing

The position, or index, of the first character is 0.

Page 4: 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,

charAt(i) tells the character at position i

char a = text.charAt(0);

char b = text.charAt(4);

char c = text.charAt(7);

char d = text.charAt(8);

‘E’

‘e’

error

‘o’

Page 5: 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,

Definition: substringAssume 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”

Page 6: 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,

Examples: substring

String a = text.substring(6,8);

String b = text.substring(0,8);

String c = text.substring(1,5);

String d = text.substring(3,3);

String e = text.substring(4,2);

“so”

“Espresso”

“spre”

error

“”

Page 7: 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,

Definition: lengthAssume 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.

Page 8: 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,

Examples: lengthString str1, str2, str3, str4;str1 = “Hello” ;str2 = “Java” ;str3 = “” ; //empty stringstr4 = “ “ ; //one space

int a = str1.length( );

int b = str2.length( );

int c = str3.length( );

int d = str4.length( );

5

4

1

0

Page 9: 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,

Definition: indexOfAssume 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.

Page 10: 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,

Examples: indexOfString str;str = “I Love Java and Java loves me.” ;

int a = str.indexOf( “J” );

int b = str.indexOf( “love” );

int c = str. indexOf( “ove” );

int d = str. indexOf( “Me” );

7

21

-1

3

3 7 21

Page 11: 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,

Examples: indexOfString str;str = “I Love Java and Java loves me.” ;

int e = str.indexOf( “J”,8 );

int f = str.lastIndexOf(“J”);

16

16

Page 12: 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,

Definition: concatenationAssume 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.

Page 13: 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,

Examples: concatenationString str1, str2;str1 = “Jon” ;str2 = “Java” ;

String a = str1 + str2;

String b = str1 + “ “ + str2;

String c = str2 + “, “ + str1;

String d = “Are you “+str1+“?”;

“JonJava”

“Jon Java”

“Java, Jon”

“Are you Jon?”

Page 14: 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,

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”

Page 15: 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,

String EqualityString a = “cat”;String b = “cat”;boolean c = (a == b);

Is c true or false?

Page 16: 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,

String EqualityString a = “cat”;String b = “cat”;boolean c = (a == b);

Is c true or false?

a -> c a ta and b are not the same

b -> c a t object, so the answer is false

Page 17: 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,

String EqualityString a = “cat”;String b = a;boolean c = (a == b);

Is c true or false?

a -> c a ta and b are the same

b object, so the answer is true, but usually this isn’twhat we have.

Page 18: 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,

String EqualityString 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.

Page 19: 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,

Input and Output• Two ways to input and output values

•Console input and output (like System.out)

•JOptionPanes (a.k.a. pop-up windows)

Page 20: 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,

Standard Output WindowA sample standard output window for displaying multiple lines of text.

• The exact style of standard output window depends on the Java tool you use.

Page 21: 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,

The print MethodWe 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.” );

Page 22: 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,

The println MethodWe 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“ );

Page 23: 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,

Standard InputThe 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).

Need to: import java.util.Scanner;

Scanner scanner = new Scanner(System.in);

int num = scanner.nextInt();

Page 24: 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,

Method Example

nextDouble( ) double d =scanner.nextDouble( );nextInt( ) int i = scanner.nextInt( );next() String str = scanner.next();nextLine() String wholeLine = scanner.nextLine();

Common Scanner Methods:

Page 25: 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,

Static ObjectsSome objects can be used without being created via “new.” Use them directly:<ClassName>.<methodName>();

Page 26: 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,

JOptionPaneA 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.

Page 27: 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,

Displaying Multiple Lines of TextWe can display multiple lines of text by separating lines with a new line marker \n.

JOptionPane.showMessageDialog(null, “one\ntwo\nthree” );

Page 28: 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,

JOptionPane for InputUsing 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.

Page 29: 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,

Type MismatchSuppose 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.

Page 30: 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,

Type ConversionWrapper 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);

Page 31: 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,

Type ConversionWorks for ints too…

String inputStr = JOptionPane.showInputDialog(null, “Enter your age”);

int age = Integer.parseInt(inputStr);

Page 32: 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,

How do you remember it all?You don’t!API: Application programming interfaceCheck it out...