Reading Lines from a String – Selected API Classes

Reading Lines from a String

The following method can be used to extract lines from a string:

Stream<String> lines()

Returns a stream of lines extracted from this string, separated by a line terminator (§16.4, p. 902). A line is defined as a sequence of characters terminated by a line terminator.

A line terminator is one of the following: a line feed character “\n”, a carriage return character “\r”, or a carriage return followed immediately by a line feed “\r\n”.

Example 8.4 uses some of these methods for reading characters from strings at (3), (4), (5), and (6). The program prints the frequency of a character in a string and illustrates copying from a string into a character array.

Example 8.4 Reading Characters from a String

Click here to view code image

public class ReadingCharsFromString {
  public static void main(String[] args) {
    int[] frequencyData = new int [Character.MAX_VALUE];    // (1)
    String str = “You cannot change me!”;                   // (2)
    // Count the frequency of each character in the string.
    for (int i = 0; i < str.length(); i++) {                // (3)
      try {
        frequencyData[str.charAt(i)]++;                     // (4)
      } catch(StringIndexOutOfBoundsException e) {
        System.out.println(“Index error detected: “+ i +” not in range.”);
      }
    }
    // Print the character frequency.
    System.out.println(“Character frequency for string: \”” + str + “\””);
    for (int i = 0; i < frequencyData.length; i++) {
      if (frequencyData[i] != 0)
        System.out.println((char)i + ” (code “+ i +”): ” + frequencyData[i]);
    }
    System.out.println(“Copying into a char array:”);
    char[] destination = new char [str.length() – 3];       // 3 characters less.
    str.getChars( 0,            7, destination, 0);         // (5) “You can”
    str.getChars(10, str.length(), destination, 7);         // (6) ” change me!”
                                                            // “not” not copied.
    // Print the character array.
    for (int i = 0; i < destination.length; i++) {
      System.out.print(destination[i]);
    }
    System.out.println();
  }
}

Output from the program:

Click here to view code image

Character Frequency for string: “You cannot change me!”
  (code 32): 3
! (code 33): 1
Y (code 89): 1
a (code 97): 2
c (code 99): 2
e (code 101): 2
g (code 103): 1
h (code 104): 1
m (code 109): 1
n (code 110): 3
o (code 111): 2
t (code 116): 1
u (code 117): 1
Copying into a char array:
You can change me!

In Example 8.4, the frequencyData array at (1) stores the frequency of each character that can occur in a string. The string in question is declared at (2). Since a char value is promoted to an int value in arithmetic expressions, it can be used as an index in an array. Each element in the frequencyData array functions as a frequency counter for the character corresponding to the index value of the element:

Click here to view code image

frequencyData[str.charAt(i)]++;                 // (4)

The calls to the getChars() method at (5) and (6) copy particular substrings from the string into designated places in the destination array, before printing the whole character array.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *