strings and i/o. lotsa string stuff… there are close to 50 methods defined in the string class. we...

Post on 18-Jan-2018

225 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

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

char b = text.charAt(4);

char c = text.charAt(7);

char d = text.charAt(8);

‘E’

‘e’

error

‘o’

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”

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

“”

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.

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

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.

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

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

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

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

16

16

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.

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?”

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 EqualityString a = “cat”;String b = “cat”;boolean c = (a == b);

Is c true or false?

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

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.

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.

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

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

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“ );

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();

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:

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

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.

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” );

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.

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.

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

Type ConversionWorks 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 interfaceCheck it out...

top related