Social Question

hungerforpizza's avatar

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

Asked by hungerforpizza (247points) 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!

Observing members: 0 Composing members: 0

5 Answers

funkdaddy's avatar

Not a java guy so this is just a guess, but it looks like you need to put some quotes around your characters to identify them as strings. Right now I’d guess they’re being treated as variables and not being found.

So instead of

case a:


case “a”:

Also remember case sensitivity with characters…

hungerforpizza's avatar

Woah, problem solved. I fixed everything and it now works. Please ignore this question.

Zyx's avatar

Damn I wasn’t going to look as I tend to stick to php but I knew this one…
Might as well mention your computer needs to be reassured what you’re saying is just gibberish it can ignore.

camertron's avatar

@funkdaddy almost hit the nail on the head. Java requires strings to be in double quotes and single characters to be in single quotes. Examples:

string hello = “Fluther Rocks!”
char gravy = ‘p’

(Side note: Java does not support strings in switch statements.)

styfle's avatar

Yes, @camertron is correct; you need to use single quotes for chars. Your compiler will scream at you if you try to use double quotes for a char. Here is a list of the data types you can use in switch statements: byte, short, char, and int primitive data types.

Answer this question




to answer.
Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther