Download presentation
Presentation is loading. Please wait.
Published byJohan Sudjarwadi Modified over 6 years ago
1
Goals Understand how to create and compare Strings.
Strings and Things Goals Understand how to create and compare Strings.
2
Learning Strategy Note taking
During the discussion, create an outline for today’s ideas Break it down by command Include an example Jot down questions in the side margin.
3
Variables in Programs Declare the variables inside the main subroutine. You can also declare variables where you need them. (Declare a looping variable at the loop) int numberOfStudents; String name; double x,y; boolean isFinished;
4
Use input.nextDouble(); for getting a double value
Simple input and math import java.util.Scanner; // program uses class Scanner public class Addition { // main method begins execution of Java application public static void main( String args[] ) // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int number1; // first number to add int number2, sum; // second number to add String name; System.out.print( "Enter first integer: " ); // prompt number1 = input.nextInt(); // read first number from user System.out.print( "Enter second integer: " ); // prompt number2 = input.nextInt(); // read second number from user System.out.print("Enter your name "); name = input.nextLine(); sum = number1 + number2; // add numbers System.out.printf( "Sum is %d\n", sum ); // display sum System.out.println("Thanks " + name);// Note: Uses the + for concatenating } // end method main } // end class Addition Use input.nextDouble(); for getting a double value
5
String Objects Unlike int, boolean and double, String is a class, and when you declare a String variable, you are declaring an object. Strings are constant, their values cannot be changed after they are created. Declarations String name = “Smith”; name is a pointer to an object that holds the name “Smith” and different methods. String river = “Mississippi”; river is a pointer to an object that holds the name “Missippi” and different methods. String name2; name2 is a pointer to an uninitialized object, to SOP is to crash. String name3 = null; name3 is a pointer to null. To SOP is to show NULL name3 = “Bob”; Moves the name3 to point to a String object with the name “Bob”.
6
We’ll take a little closer look at each of these.
Some String Methods We’ll take a little closer look at each of these. .length(); Returns the length (number of characters) of the string, int size = name.length(); .equals(); // Inherited (extended) from the Object class if (name.equals(name2)) .compareTo();//Inherited (implemented) from Comparable if (name.compareTo(name2) < 0) .substring(); Returns a part of a string. name2 = name1.substring(3); name3 = name1.substring(2,2); .indexOf() name.indexOf(substring, beginIndex) int spot = name.indexOf(“Mr.”); int spot2 = name.indexOf(lastName, 3);
7
int size = name.length();
Returns the length of the strength. String name = “Smith”; name is a pointer to an object that holds the name “Smith” and different methods. Example int ans = name.length();
8
if name.equals(name2) Returns a true if name equals name2
String name = “Smith”; name is a pointer to an object that holds the name “Smith” and different methods. String name2 = “Jones”; if (name.equals(name2)) System.out.println(“The names are the sames”); What about… if (name == name2) Since name and name2 are both objects, this compares the addresses of the objects, not the values of the objects.
9
if name.compareTo(name2)
This compares the values of the name object with the value of the name2 object with the following values returned. If name comes before name2, a negative value is returned. If name comes after name2, a positive value is returned. If name and name2 are an exact match, it returns 0.
10
Review What is a String? Name three String methods?
What is different between null and nothing? How do you compare strings? String: a class for defining String objects. String objects hold words. String methods so far:length(), toUpperCase(), .equals(), .compareTo() Compare strings using the .equals) and the toUpper() methods.
11
Note: The first character is at position 0.
name.substring(n) Copies from the nth position to the end. String name = “West Salem Titans”; String name2 = name.substring(5); Sets name2 to “Salem Titans”; String name3 = name.substring(n, m); Copies from the nth to the (m-1)th positions. name3 = name.substring(5, 10); Sets name3 to “Salem”
12
name.indexOf(substring, beginIndex)
int ans = name.indexOf(substring); Returns the first index (position) of substring in the string. int ans = name.indexOf(substring, beginIndex); Returns the first index of substring in the string, starting at the beginIndex String name = “West Salem Titans”; int pos = name.indexOf(“e”); Sets pos to 1, the index of the first occurrence of “e”
13
public class FirstString
{ public static void main (String[] args) String mySentence = "this is a trial"; String anyString = " "; System.out.println(mySentence); System.out.println(anyString); mySentence +="Java is great."; anyString+= " West Salem High School"; mySentence = mySentence; mySentence = mySentence ;
14
if (mySentence.equals(anyString))
// mySentence.equalsIgnoreCase() for not case sensitive System.out.println("The same."); else System.out.println("Different."); if (mySentence.compareTo(anyString)<0) System.out.println("First less"); else if (mySentence.compareTo(anyString)==0) System.out.println("Equal"); System.out.println("First is more."); for (int pos = 0; pos<mySentence.length(); pos++) System.out.println(mySentence.substring(pos, pos+1)); System.out.println(mySentence); System.out.println(mySentence.substring(15)); System.out.println("s occurs in the #" + mySentence.indexOf('s') + " position");
15
Strings review Strings are Objects (Variables with methods)
Use a capital ‘S’ when declaring as a String. Some common String methods.
16
Review Write a summary of today’s topics What have you learned?
New methods What makes sense What doesn’t make sense Questions
17
String Program Options
Complete any Two of the following. String Program Options Find last name Input first and last name into one String Output only the last name. Push: Support middle names Write a mad-lib program Have the user enter at least 4 pieces of information. Noun, Adverb (honestly), Adjective (messy), Verb, Geographical location, … The computer will generate the mad-lib English to Pig-Latin translation program. The rules of Pig Latin are If the word begins with a consonant -- such as ``string,'' ``Latin'' -- divide the word at the first vowel, swapping the front and back halves and append ``ay'' to the word -- ``ingstray,'' ``atinLay.'' If the word begins with a vowel -- such as ``am,'' ``are,'' ``i'' -- append ``yay'' to the word -- ``amyay,'' ``areyay,'' ``iyay.'' If the word has no vowels (other than 'y') -- such as ``my,'' ``thy'' -- append ``yay'' to it -- ``myyay,'' ``thyyay.'' Level 1, Translate a single word Level 2: Translate a sentence. Input a word, output whether or not it is a palindrome.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.