Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 Strings and I/O

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

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

4 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’ ‘e’‘e’ error ‘o’‘o’

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

6 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 “”

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

8 Examples: length String str1, str2, str3, str4; str1 = “Hello” ; str2 = “Java” ; str3 = “” ; //empty string str4 = “ “ ; //one space int a = str1.length( ); int b = str2.length( ); int c = str3.length( ); int d = str4.length( ); 5 4 1 0

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

10 Examples: indexOf String 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 3 3721

11 Examples: indexOf String str; str = “I Love Java and Java loves me.” ; int e = str.indexOf( “J”,8 ); int f = str.lastIndexOf(“J”); 16

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

13 Examples: concatenation String str1, str2; str1 = “Jon” ; str2 = “Java” ; String a = str1 + str2; String b = str1 + “ “ + str2; String c = str2 + “, “ + str1; String d = “Are you “+str1+“?”; “JonJava” “Java, Jon” “Are you Jon?”

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

15 String Equality String a = “cat”; String b = “cat”; boolean c = (a == b); Is c true or false?

16 String Equality String a = “cat”; String b = “cat”; boolean c = (a == b); Is c true or false? a -> c a t a and b are not the same b -> c a tobject, so the answer is false

17 String Equality String a = “cat”; String b = a; boolean c = (a == b); Is c true or false? a -> c a t a and b are the same b object, so the answer is true, but usually this isn’t what we have.

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

19 Input and Output Two ways to input and output values Console input and output (like System.out) JOptionPanes (a.k.a. pop-up windows)

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

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

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

23 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). Need to: import java.util.Scanner; Scanner scanner = new Scanner(System.in); int num = scanner.nextInt();

24 MethodExample nextDouble( )double d =scanner.nextDouble( ); nextInt( )int i = scanner.nextInt( ); next() String str = scanner.next(); nextLine()String wholeLine = scanner.nextLine(); Common Scanner Methods:

25 Static Objects Some objects can be used without being created via “new.” Use them directly:. ();

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

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

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

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

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

31 Type Conversion Works for ints too… String inputStr = JOptionPane.showInputDialog( null, “Enter your age”); int age = Integer.parseInt(inputStr);

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


Download ppt "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,"

Similar presentations


Ads by Google