Send to a Friend

hungerforpizza's avatar

How to use a Character in a Switch Statement (Java)

Asked by hungerforpizza (247 points ) November 21st, 2010

Here is my prompt for the assignment:

Create a program that uses a switch statement to determine what type of music a given user likes. The program will output a genre of music depending on what group the user enters. For example, if the user types the letter ‘a’ (Korn), the program will output Rock. Include a list (at least four) of groups for the user to choose from. Make sure to add a default case if the user types in a choice that isn’t listed. The Controlling Statement must be of type char.

Here is my code so far:

import javax.swing.JOptionPane;

public class Q3 {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

String groupstring =
JOptionPane.showInputDialog(“Based on an artist you pick, this program will pick a type of music you enjoy.” + ”\n” + “to choose one of the following groups, type in the letter that corresponds to it and press OK” + ”\n” + “A = The Bravery” + ”\n” + “B = Johnny Cash” + ”\n” + “C = Metallica” + ”\n” + “D = Johann Strauss” );

switch (group) {

case a:

JOptionPane.showMessageDialog(null, “You would like rock Music.”);

case b:

JOptionPane.showMessageDialog(null, “You would like country Music.”);

case c:

JOptionPane.showMessageDialog(null, “You would like metal Music.”);

case d:

JOptionPane.showMessageDialog(null, “You would like classical Music.”);

JOptionPane.showMessageDialog(null, “Invalid choice. Please run the program again.”);



And I am getting errors. I know how to use a Switch statement with Integers, but it get’s angry when I use characters. Help would be appreciated!

Using Fluther


Using Email

Separate multiple emails with commas.
We’ll only use these emails for this message.