150 likes | 284 Views
Characters and Strings. Characters and Strings. String class. Defined in java.lang.String String aGreeting = new String(“Hello”); OR String aGreeting = “Hello”;. Comparing Strings. String variable name is a reference to a location in memory String aGreeting = “Hello”;
E N D
Characters and Strings Characters and Strings
String class • Defined in java.lang.String • String aGreeting = new String(“Hello”); OR • String aGreeting = “Hello”;
Comparing Strings • String variable name is a reference to a location in memory String aGreeting = “Hello”; aGreeting = “Bonjour”; aGreeting holds an address that points to “Hello” aGreeting holds a new address that points to “Bonjour” The garbage collector will discard “Hello” Strings and other objects that can’t be changed are known as immutable
The equals() method • Evaluates the contents of two String objects to see if they are the same • Method returns boolean true if equivalent if(aName.equals(anotherName))
The equalsIgnoreCase() method • Evaluates the contents of two String objects to see if they are the same • Ignores the case of the letters aName = “DEBBIE”; anotherName = “Debbie”; if(aName.equalsIgnoreCase(anotherName)) Returns true
The compareTo() method • Returns: 0 if strings are equal negative # calling “less than” argument positive # I calling “greater than” argument • Example: aName = “Roger”; aName.compareTo(“Robert”) 5 • For alphabetical order: if(aWord.compareTo(anotherWord) < 0) aWord = hamster; anotherWord = iguana Returns true
Other String Methods • toUpperCase() aWord = “something”; aWord = aWord.toUpperCase(); • toLowerCase() • indexOf() String myName = “Stacy”; myName.indexOf(‘a’); myName.indexOf(‘q’); • charAt() myName.charAt(0); Results?
Other String Methods • endsWith() myName.startsWith(“Sta”) returns True • replace() String yourName = “Annette”; String goofyName = yourName.replace(‘n’, ‘X’); AXXette
toString() Method • Converts primitive type to a String • How do we know that toString() is an overloaded method? int someInt = 4; theString = Integer.toString(someInt); “4”
subString() Method • subString(start position, end position) String[] dayOfWeek = “Monday”, “Tuesday”, “Wednesday”, ”Thursday”, ”Friday”}; String sentence; int x; for(x = 0; x < dayOfWeek.length; ++x) { sentence = “The abbreviation for “ + dayOfWeek[x] + “ is “ + dayOfWeek[x].substring(0,3); System.out.println(sentence); }
Converting Strings to Integers • To convert a String to an integer, use the Integer class • parseInt() is a method of the Integer class anInt = Integer.parseInt(“649”);
Converting Strings to Doubles • To convert a String to a double, use the Double class • double dvalue = Double.parseDouble(“147.82”) • valueOf()and doubleValue() are methods of the Double class String stringValue = new String(“147.82”); Double tempValue = Double.valueOf(stringValue); double doubleValue = tempvalue.doubleValue();
Example of Input import java.swing.x; public class NumIn { public static void main(String[ ] args) { String inputString; int inputNumber; inputString = JOptionPane.showInputDialog(null, “Enter the number”); inputNumber = Integer.parseInt(inputString); if (inputNumber > 100) JOptionPane.showMessageDialog(null, “A surchage will apply”); System.exit(0); } }