Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with switch-statement & ä, ö, ü

807580Jun 28 2010 — edited Jul 4 2010
Hi all,

I am doing this Java online tutorial right now and have a problem with one of the exercises. Hopefully you can help me:

I have to write a program that determines the number of consonants, vowels, punctuation characters, and spaces in an input line. I found a solution, but have two questions about it:

• I’m unable to calculate the amount of umlauts (ä, ö, ü). Somehow the program doesn’t recognize those characters. Why?
• In general I’m not very happy with this huge list of “cases”. How would you solve a problem like this? Is there a more convenient/elegant way?

Thanks in advance!
/*
Write a program that determines the number of consonants, vowels, punctuation characters, and spaces in an input line. 
Read in the line into a String (in the usual way). Now use the charAt() method in a loop to access the characters one by one. 
Use a switch statement to increment the appropriate variables based on the current character. After processing the line, print out 

the results.
*/

import java.util.Scanner;

class Kap43A1
{
  public static void main ( String[] args )
  {
    String line;
    char letter;
    int total, countV=0, countC=0, countS=0, countU=0, countP=0;
    
    Scanner scan = new Scanner(System.in);
    System.out.println( "Please write a sentence " );
    line = scan.nextLine();

    total=line.length(); //Gesamtanzahl an Zeichen des Satzes

    for (int counter=0; counter<total; counter++)
    {
      letter = line.charAt(counter); //ermitteln des Buchstabens an einer bestimmten Position des Satzes

      switch (letter)
      {
        case 'A': case 'a':
        case 'E': case 'e':
        case 'I': case 'i':
        case 'O': case 'o':
        case 'U': case 'u':
          countV++;
          break;

        case 'B': case 'b': case 'C': case 'c': case 'D': case 'd': case 'F': case 'f': case 'G': case 'g': case 'H': case 'h':
        case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': case 'M': case 'm': case 'N': case 'n': case 'P': case 'p':
        case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': case 'T': case 't': case 'V': case 'v': case 'W': case 'w':
        case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z':
          countC++;
          break;

        case ' ':
          countS++;
          break;

        case ',': case '.': case ':': case '!': case '?':
          countP++;
          break;

        case 'Ä': case 'ä': case 'Ö': case 'ö': case 'Ü': case 'ü':
          countU++;
          break;
      }
    }

    System.out.println( "Total amount of characters:\t" + total );
    System.out.println( "Number of consonants:\t\t" + countC );
    System.out.println( "Number of vocals:\t\t" + countV );
    System.out.println( "Number of umlauts:\t\t" + countU );
    System.out.println( "Number of spaces:\t\t" + countS );
    System.out.println( "Number of punctuation chars:\t" + countP );
  } 
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 1 2010
Added on Jun 28 2010
9 comments
576 views