9.2 The String Class

  • Docx File 41.20KByte



Chapter 9 Strings and Text I/O9.2 The String ClassA string is a sequence of characters. Strings are objects in Java. There are several ways to create a string.Assign a value.String message1 = "Hello";Use the keyword new (but it's just extra work).String message = new String("Hello");Create a new String using a charArray as the parameter to the constructor.char[] charArray = {'H', 'e', 'l', 'l', 'o'};String message = new String (charArray);Example code:Create a new project called CH09-String-Demos. Then add the following code: String message1 = "Hello"; String message2 = new String("HELLO"); // Convert a char array to a string: char[] charArray = {'H', 'e', 'l', 'l', 'o'}; String message3 = new String (charArray); System.out.println (message1); System.out.println (message2); System.out.println (message3);Strings are immutable – they cannot be changed. If you have the following:String pres = "Washington";pres = "Adams";The "Washington" String is thrown away and a new String with the value "Adams" is created. The String class has no methods for modifying a String object. Since strings are objects, when we use the six relational operators: ==, !=, >, <, >=, <= all we are comparing is the addresses of the strings! To compare strings, use one of the following methods:equals(String otherString)equalsIgnoreCase(String otherString)compareTo(String otherString)compareToIgnoreCase(String otherString)The two equals methods return true or false.The two compare methods return 0 if the strings are equal, a negative number if the invoking string is less than otherString, and a positive number if the invoking string is greater than otherString. It's easy to remember: if a is > b, you get a result that is greater than 0. If a is < b, you get a result that is less than 0.Sample code:System.out.println ("Should be false: " + message1.equals(message2));System.out.println ("Should be true: " + message1.equals(message3));System.out.println ("Should be true: " + message1.equalsIgnoreCase(message2));String a="a", b="b", A="A", B="B";System.out.println ("Should be negative: " + pareTo(b));System.out.println ("Should be positive: " + pareTo(a));System.out.println ("Should be zero: " + pareTo(a));System.out.println ("Should be negative: " + pareTo(B));System.out.println ("Should be positive: " + pareTo(A));// A is code 65, a is code 97System.out.println ("Should be negative: " + pareTo(a));System.out.println ("Should be positive: " + pareTo(A));System.out.println ("Should be zero: " + pareToIgnoreCase(A));length()The length method returns the number of characters in the string.lengthThe length property (no parentheses) returns the number of entries in an array. Does not work on strings. Confusing.charAt(index)The charAt method returns the character (data type char) at the location in the string. String locations are zero-based, so a 5-character string has characters at locations 0 through 4.System.out.println ("Should be 5: " + message1.length());for (int i=0; i<message1.length(); i++) System.out.println (message1.charAt(i));substring(int beginIndex), substring(int beginIndex, int endIndex)The substring method returns a substring. It has two versions:// returns all characters to the end of the string substring(int beginIndex) // returns all characters from beginIndex up to endIndex – 1.substring(int beginIndex, int endIndex)Sample code System.out.println ("Should be 'ello': " + message1.substring(1)); System.out.println ("Should be 'ell': " + message1.substring(1,4));More common string methodstoLowerCase() returns a lower-case version of the given string.toUpperCase() returns an upper-case version of the given string.trim() returns a copy of the string with leading and trailing spaces removed.split(String delimiter) returns an array of strings. This is very useful when trying to separate the tokens in a string. Note that the delimiter must be a regular expression. The Java syntax for regular expressions requires that all characters be inside of square brackets. We will not go into regular expressions, but if you want a simple list of characters that can be used as delimiter characters, you must enclose all of the characters within square brackets. For example to break a string wherever a space, a comma, or a tab occurs, use this (note that there is a space before the comma)[ ,\t]Sample codeString sentence = "Four score and seven years ago, our fathers\tbrought forth";String[] wordList = sentence.split("[ ,\t]");for (int i=0; i<wordList.length; i++) System.out.printf ("%4d: %s\n", i, wordList[i]);Converting between strings and arraysTo convert a string to a char array (so you can modify the individual characters), use the toCharArray method.Sample code char[] charList = message1.toCharArray(); for (int i=0; i<charList.length; i++) System.out.println (charList[i]);9.2.10 Converting Characters and Numeric values to StringsTo convert number variables to strings (assume that i is an int, and d is a double). Note that Double and Integer are the names of classes:Integer.toString(i)Double.toString(d)To convert an array of characters to a string, the following returns a string made up of the given characters. String s = new String(charArray) To convert a string to a number:Double.parseDouble(String s)Integer.parseInt(String s)Sample code double d = Double.parseDouble("12345.6789"); System.out.printf("Should be 12345.6789: %f\n", d); int i = Integer.parseInt("12345"); System.out.printf("Should be 12345: %d\n", i);9.2.11 Formatting StringsTo print formatted strings, use the static method:String.format(formatString, str1, str2, …)The format string is the same as used in the printf method. A format string can contain zero, one, or more?format specifiers. The general form of a format specifier is:?%[argument_index$][flags][width][.precision]conversionwhere things in square brackets are optional, and conversion is a character indicating the?conversion?to be applied to the corresponding variable value. The only required characters in the format specifier are the percent sign?%?and the?conversion?character (‘d’ for integer, ‘f’ for float, ‘s’ for string).?Sample code?System.out.println(String.format("Hi %s, you owe me $%5.2f.", "Jack", 25.));9.2.13 Problem: Converting Hexadecimals to DecimalsThis is an interesting problem. Note: Start at the left and convert the hex character to its decimal value. Every time you move one character to the right, you multiply the current value by 16.Some examples of converting hex to decimal:Convert 12F to decimal:Pick off the '1'. Convert to the number 1. Add to result.Pick off the '2'. Convert to the number 2. Multiply the result (1) by 16 (=16) and add 2 (=18).Pick off the 'F". Convert to the number 15. Multiply result (18) by 16 (=288) and add 15 (=303).Here is the code:public static int hexToDecimal(String hex){ hex = hex.toUpperCase(); int n = 0; for (int i=0; i<hex.length(); i++) if (hex.charAt(i) >= '0' && hex.charAt(i) <= '9') { n = n * 16 + (hex.charAt(i) - '0'); System.out.println ("n: " + n); } else if (hex.charAt(i) >= 'A' && hex.charAt(i) <= 'F') { n = n * 16 + (hex.charAt(i) - 'A' + 10); System.out.println ("n: " + n); } return n;}In-class exercise:Above we looked at a program to convert a hex string to a decimal integer. Write a method that goes the other way: it accepts a decimal value (int) and returns the hex equivalent (String).NOTE: My function was called intToHex, but I also used a helper function called hexDigit(n) that accepts a number from 0-15 and returns a 1-character string that is the hex value of the number.Some examples of how to convert from int to hex:Convert 47 to hex.Initialize hex string to "".Divide n by 16 and take the remainder. The remainder is 15. Convert to a hex string: "F". Append to the left of our string.Divide n by 16. The quotient is 2. This is the new value of n.Divide n by 16 and take the remainder. The remainder is 2. Convert to a hex: "2" string. Append to the left of our string.Divide n by 16. The quotient is 0. This is the new value of n. Quit.Convert 1000 to hexInitialize hex string to "".Divide n by 16 and take the remainder. The remainder is 8. Convert to a hex string: "8". Append to the left of our string.Divide n by 16. The quotient is 62. This is the new value of n.Divide n by 16 and take the remainder. The remainder is 14. Convert to a hex string: "E". Append to the left of our string.Divide n by 16. The quotient is 3. This is the new value of n.Divide n by 16 and take the remainder. The remainder is 3. Convert to a hex string: "3". Append to the left of our string.Divide n by 16. The quotient is 0. This is the new value of n. Quit.From these examples, derive the algorithm and implement it as a function.9.5 The Character ClassMany methods in the Java API require that an object be passed to it. And almost everything in Java is an object. However, there are four data types that are NOT objects: ints (all sizes), double (and float), boolean, and char.However, Java also provides a way to treat each of these data types as objects, too! These classes are called wrapper classes because they "wrap" the primitive data inside of an object. The wrapper class for the char primitive is the Character class.Most of the methods of the Character class are static, which means that most of the time you won't have to create a Character object. Some useful methods:Character.isLetter(char ch)Character.isLetterOrDigit(char ch)Character.toString(char ch)Character.isLowerCase(char ch)Character.isUpperCase(char ch)Character.toUpperCase(char ch)Character.toLowerCase(char ch)Note that all of the above methods are invoked by using the name of the Character class—not by invoking an object (because they are static methods).Examplesmessage1 = "Hi, Jack, you owe me $25.00";System.out.println(message1);for (i=0; i<message1.length(); i++){ System.out.println (message1.charAt(i) + " is letter: " + Character.isLetter(message1.charAt(i))); System.out.println (message1.charAt(i) + " is letter or digit: " + Character.isLetterOrDigit(message1.charAt(i))); System.out.println (message1.charAt(i) + " is lowercase: " + Character.isLowerCase(message1.charAt(i))); System.out.println (message1.charAt(i) + " is uppercase: " + Character.isUpperCase(message1.charAt(i))); System.out.println (message1.charAt(i) + " to uppercase: " + Character.toUpperCase(message1.charAt(i))); System.out.println (message1.charAt(i) + " to lowercase: " + Character.toLowerCase(message1.charAt(i)));}In-class exerciseWrite a program that will read a string from the keyboard and tell the user how many upper-case letters, how many lower-case letters, how many digits, and how many "other" characters were in the string.9.6 The StringBuilder classStrings are immutable – they cannot be changed. If you have the following:String pres = "Washington";pres = "Adams";The "Washington" String is thrown away and a new String with the value "Adams" is created. The String class has no methods for modifying a String object. It has many methods for creating a new String object. Modifying a StringBuilderIf you want to modify a string (which you frequently do), you should use the StringBuilder class. The StringBuilder class has most of the same methods as the String class has. It also has the following methods for modifying a StringBuilder object:setCharAt(int index, char character)append(String s)insert(int index, String s)delete(int start, int end)deleteCharAt(int index)replace(int start, int end, String s)Note: on delete and replace, the end variable is actually one more than the ending position. So, if you want to delete the 3 characters at positions 3, 4, and 5 in a string s, you would have to do this:s.delete(3, 6)One way to remember this is that if you subtract the two argument values, you get the number of characters that are deleted.Also, all of the methods change the object, AND they return a reference to the object. Most of the time you don't need the reference, though.To convert a StringBuilder object to a String (assume s is a String object and sb is a StringBuilder object):s = sb.toString();Sample code public static void testStringBuilder() { StringBuilder s = new StringBuilder("hello"); System.out.println("Original string: " + s); s.setCharAt(0, 'H'); System.out.println ("Capitalize the 'h': " + s); s.append( ", how are you?"); System.out.println ("Add ', how are you?': " + s); s.insert(5, " George"); System.out.println ("Insert 'George ': " + s); s.replace(6,12, "Thomas"); System.out.println ("Replace 'George' with 'Thomas': " + s); s.deleteCharAt(12); System.out.println ("Delete the comma: " + s); s.delete(6, 13); System.out.println ("Delete the word 'Thomas': " + s); }Some other methods that do not change a StringBuilder objectcapacity(). Returns the current capacity of the StringBuilder (max length).length(). Returns the current number of characters in the StringBuilder.charAt(int). Returns the character at the given location.Sample codeAdd the following to the bottom of your testStringBuilder method.System.out.println ("Capacity is: " + s.capacity());9.5 Command Line ArgumentsYou can pass strings to a main method from the command line, like this:java TestMain arg0, arg1, arg2The values arg0, arg1, and arg2 are passed in as the String[] args parameters to the main method. They can be accessed like this:arg[0]arg[1]arg[2]If you are using the IntelliJ IDE and not the command line to run your program, you can set the command line arguments like this:Click on Run.Click on Edit Configurations…In the Run/Debug Configurations window. If necessary, on the left sideclick on Application, then on Main.Enter the arguments in the Arguments text box. Separate them with either spaces or commas. If you want to pass in a string with spaces in it, use quotation marks.Click on OK.The following code will determine the number of command line arguments and print them out.System.out.printf("Number of command line arguments: %d.\n", args.length);for (int i=0; i<args.length; i++) System.out.printf("Command line argument #%d: %s\n", i, args[i]);Check out the Java calculator program (Listing 9.5).9.6 The File ClassThe File class "is intended to provide an abstraction that deals with most of the machine-dependent complexities of files and path names in a machine-independent fashion." Some common File class methods are listed in section 9.6. Note that the File class does not contain methods for reading or writing data to a file!9.7 File Input and Output9.7.1 Creating an output fileThe following creates an output file (in the root level of the project) called scores.txt. Note that it uses fully-qualified names for File and PrintWriter. If this was not done, we would have to use an import statement (which is what would normally be done!): import java.io.*;Or, we could do this:import java.io.File;import java.io.PrintWriter;Create a new project called CH09-File-Demo. Here is the main program: public static void main(String[] args) throws Exception { java.io.File file = new java.io.File("scores.txt"); if (file.exists()) { System.out.println("File already exists"); System.exit(0); } // Create a file java.io.PrintWriter output = new java.io.PrintWriter(file); // Write formatted output to the file output.print("John T Smith "); output.println(90); output.print("Eric K Jones "); output.println(85); // Close the file output.close(); }9.7.2 Reading Data from an input file using a Scanner objectTo read from the keyboard, we create a Scanner object for System.in (the keyboard).To read from a file, we create a Scanner object for a File object.To create a File object:import javax.swing.*; // For the chooser dialog boximport java.io.*; // For the File classimport java.util.*; // For the Scanner class... // Ask the user to select a file for input JFileChooser chooser = new JFileChooser();// Add path str as argument chooser.showOpenDialog(null); // Create a file object based on the user's choice File theFile = chooser.getSelectedFile();Then, to create a Scanner for the file: Scanner input = new Scanner(theFile);Once the scanner has been created, you can read from it using the following methods:nextInt()nextDouble()nextLine()When done, close the file:inFile.close();Code to read a file of strings into an array of strings:Use the following data and write code to create 3 files: names only, scores only, both names and scores.Luis Arroyo 1Tex Clevenger 2Al Downing 3Whitey Ford 4Johnny James 5Bill Stafford 6Ralph Terry 7Elston Howard 8Clete Boyer 9Tony Kubek 10Bobby Richardson 11Bill Skowron 12Earl Torgeson 13Tom Tresh 14Yogi Berra 15Bob Cerv 16Hector Lopez 17Mickey Mantle 18Roger Maris 19Jack Reed 20Jesse Gonder 21Lee Thomas 22Note: Any method that does file i/o must either put all file operations inside of a try/catch block, or it must add throws Exception to the end of the method declaration line. If you were writing a program for others to use, you would want to make it as bulletproof as possible and you would use try/catch. We will not do so in this class unless told otherwise. We will simply append throws Exception to the end of the method declaration. Note that if this is done in a method other than main, that you must also include throws Exception in the calling method (and its calling method, etc.). public static void main(String[] args) throws Exception { readStringFile(); readIntFile(); readStringsAndIntsFile(); } public static void readStringFile() throws Exception { // Ask the user to select a file for input JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); // Create a file object based on the user's choice File theFile = chooser.getSelectedFile(); Scanner input = new Scanner(theFile); String[] name = new String[100]; //Assume no more than 100 lines int i = 0; while (input.hasNext()) { name[i] = input.nextLine(); i++; } int lineCount = i; // Now print the array System.out.println ("\nNames"); for (i=0; i<lineCount; i++) System.out.println (name[i]); } // Code to read a file of ints into an array of ints: public static void readIntFile() throws Exception { // Ask the user to select a file for input JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); // Create a file object based on the user's choice File theFile = chooser.getSelectedFile(); Scanner input = new Scanner(theFile); int[] score = new int[100]; //Assume no more than 100 lines int i = 0; while (input.hasNext()) { score[i] = input.nextInt(); i++; } int lineCount = i; // Now print the array System.out.println ("\nScores"); for (i=0; i<lineCount; i++) System.out.println (score[i]); } // Code to read a file of strings and ints into an array of strings and intspublic static void readStringsAndIntsFile() throws Exception{ // Ask the user to select a file for input JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); // Create a file object based on the user's choice File theFile = chooser.getSelectedFile(); Scanner input = new Scanner(theFile); String theLine; String[] name = new String[100]; //Assume no more than 100 lines int[] score = new int [100]; int i = 0; while (input.hasNext()) { theLine = input.nextLine(); String tokens[] = theLine.split("[ \t]+"); name[i] = tokens[0] + " " + tokens[1]; score[i] = Integer.parseInt(tokens[2]); i++; } int lineCount = i; // Now print the array System.out.println ("\nNames and Scores"); for (i=0; i<lineCount; i++) System.out.println (String.format("%20s %5d", name[i], score[i]));}Reading an input file with multiple data items on each line.Open the program from last class (program that read lines of strings from a data file). Let's modify it to make it split the line into a name and a score. The easiest way to do this is to use the Scanner object, which splits a string into "tokens" delimited by white space (spaces, tabs, carriage returns).Code to read a file of strings and ints into an array of strings and ints public static void readStringsAndIntsFile() throws Exception { Scanner input = new Scanner("John T Smith 90 Eric K Jones 85"); String[] firstName = new String[10]; String[] middleInitial = new String[10]; String[] lastName = new String[10]; int[] score = new int [10]; int i = 0; while (input.hasNext()) { firstName[i] = input.next(); middleInitial[i] = input.next(); lastName[i] = input.next(); score[i] = input.nextInt(); i++; } int lineCount = i; // Now print the array System.out.println ("\nNames and Scores"); for (i=0; i<lineCount; i++) System.out.println (String.format("%15s %1s %15s %5d", firstName[i], middleInitial[i], lastName[i], score[i])); }You can define your own delimiter character(s). Make the following changes: Scanner input = new Scanner("John/T/Smith/90/Eric/K/Jones/85"); // Put delimiter characters in square brackets input.useDelimiter("[/]");You can check for multiple delimiter characters: Scanner input = new Scanner("John T/Smith^90 Eric/K^Jones/85"); // Put delimiter characters in square brackets input.useDelimiter("[ /^]");You can also use control characters (like tab, carriage return): Scanner input = new Scanner("John T\tSmith\t90\nEric K\tJones\t85"); // Put delimiter characters in square brackets input.useDelimiter("[ /\n\t]");Note that the above looks for exactly ONE of the delimiter characters. If there are two, then the scanner assumes that there are two tokens! So if you have two blanks separating two tokens, you MAY get an error. I have added two spaces before the word "Eric": Scanner input = new Scanner("John T/Smith^90 Eric/K^Jones/85"); // Put delimiter characters in square brackets input.useDelimiter("[ /^]");This (above) generates an error only because we are now trying to read the word "Jones" into an array of integers!If we want to tell the scanner to treat multiple delimiters as a single delimiter (frequently there may be many spaces between tokens), we need to put a plus sign after the list of delimiters, like this: Scanner input = new Scanner( "John^T^Smith^^^^^^90 Eric\t K^Jones//////85"); // Put delimiter characters in square brackets input.useDelimiter("[ /^\t]+");The "+" means "one or more of the preceding".Special characters in the argument to useDelimiter are:[] anything in square brackets means "pick one"\t = tab\n = new line\r = carriage return+ = one or more of the preceding ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download