Social Question
How to use a Character in a Switch Statement (Java)
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.”);
System.exit(0);
case b:
JOptionPane.showMessageDialog(null, “You would like country Music.”);
System.exit(0);
case c:
JOptionPane.showMessageDialog(null, “You would like metal Music.”);
System.exit(0);
case d:
JOptionPane.showMessageDialog(null, “You would like classical Music.”);
System.exit(0);
default:
JOptionPane.showMessageDialog(null, “Invalid choice. Please run the program again.”);
System.exit(0);
}
}
}
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!