JMU
Strings in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Getting Started
Immutability
Immutability (cont.) - "Changing" Case
Immutability (cont.) - "Changing" Case
Concatenation
Concatenation (cont.)
Concatenation (cont.)
Concatenation (cont.)
Concatenation (cont.)
Why Are We Suddenly Using the new Operator?
Why Are We Suddenly Using the new Operator? (cont.)
Some String Methods
Some String Methods (cont.)
Examples

Counting Vowels

javaexamples/strings/VowelCounter.java
/**
 * Count the number of vowels in a String
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class VowelCounter
{
    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
       char               letter;       
       int                n, vowels;
       String             line;

       JMUConsole.open();

       // Prompt for and read the text
       JMUConsole.printf("Enter the text: ");
       line = JMUConsole.readLine();

       // Get the length of the String (in characters)
       n = line.length();
       
       // Initialize the accumulator
       vowels = 0;
       
       // Loop through the String
       for (int i=0; i<n; i++)
       {
          // Get the next letter
          letter = line.charAt(i);           

          // Check if it's a vowel (this can be done more efficiently)
          if ((letter == 'a') || (letter == 'e') || (letter == 'i') ||
              (letter == 'o') || (letter == 'u') ||
              (letter == 'A') || (letter == 'E') || (letter == 'I') ||
              (letter == 'O') || (letter == 'U')                      )
          {
             vowels++;             
          }
       }
       
       // Print the answer
       JMUConsole.printf("Vowels: %d\n", vowels);

       JMUConsole.close();
    }
}
        
Examples (cont.)

A Hex Converter

javaexamples/strings/HexConverter.java
/**
 * Convert a "number" in hexadecimal to a number in base 10
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class HexConverter
{
    /**
     * The entry point
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
       char               digit;
       int                i, length, number, power, value;
       String             hex;

       JMUConsole.open();

       // Read the hexadecimal number
       JMUConsole.printf("Enter the number in base 16: ");
       hex = JMUConsole.readLine();

       // Clean-up the hex number (Note that these methods
       // cannot be void since String objects are immutable.)
       hex = hex.trim();
       hex = hex.toUpperCase();
        

       // Initialization
       length = hex.length();
       number = 0;
       power  = 1;


       // Process each character (This can be done more efficiently)
       for (i=length-1; i>=0; i--) 
       {
          digit  = hex.charAt(i);
          value  = Character.digit(digit, 16);

          number = number + (value * power);

          power  = power * 16;
       }

       JMUConsole.printf("Base 10: %d\n", number);
       JMUConsole.close();
    }
}