Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Implementing the switch statement

1. ## Implementing the switch statement

This is the problem I have to do for a lab. I am not asking anyone to do the work for me but if they could just start it because I have no idea and my textbook doesn't help me very much with this one. I have been working on it for a week and can't even get started. I just need an example of how it would look or just how to switch the suffixes and have everything print. I have no idea how to start the switch statement. Thanks for any help given!

2. Design and implement an application that prints the verses of the song “The Twelve Days of Christmas”, in which each verse adds one line. The two verses of the song are:
On the 1st day of Christmas my true love gave to me
A partridge in a pear tree
On the 2nd day of Christmas my true love gave to me
Two turtles doves and,
A partridge in a pear tree
Use a switch statement in a loop to control which lines get printed. Hint: Order the cases carefully and avoid the break statement. Use a separate switch statement to put the appropriate suffix on the day number (1st, 2nd, 3rd, etc.).

2. ## Re: Implementing the switch statement

Well for starters, are you familiar with how switch statements work?

4. ## Re: Implementing the switch statement

i did look at that but I don't understand how I can print the first line, then the second line but changing it to "2nd day" and reprinting the first line.

5. ## Re: Implementing the switch statement

Look at what order the song lines are in as new lines are added:
1
then
2
1
then
3
2
1

Then think what happens in a switch statement when a case statement does not have a break:
Hint: it falls through and does the next case that is below it.

6. ## Re: Implementing the switch statement

So the switch statement without a break will print 3 when i call it and then it will also print 2 and 1 after it or do I need to print them backwards so 12 is the first case and 1 is the last case and call the last case first so it will then print the last one, then the second to last and the last, and so on?

7. ## Re: Implementing the switch statement

Write a small program and try it to see what happens.

8. ## Re: Implementing the switch statement

```public class StudentInformation
{
public static void main (String [] args)
{
String x;
for (int n = 12; n >= 1; n--)
{
System.out.println ("On the " + n + " day of Christmas my true love gave to me");
System.out.println (x);

switch (x)
{
case 1 : System.out.println ("Twelve drummers drumming,");
case 2 : System.out.println ("Eleven pipers piping,");
case 3 : System.out.println ("Ten lords a leaping,");
case 4 : System.out.println ("Nine ladies dancing,");
case 5 : System.out.println ("Eight maids a milking,");
case 6 : System.out.println ("Seven swans a swimming,");
case 7 : System.out.println ("Six gesse a laying,");
case 8 : System.out.println ("Five golden rings,");
case 9 : System.out.println ("Four calling birds,");
case 10 : System.out.println ("Three french hens,");
case 11 : System.out.println ("Two turtle doves and,");
case 12 : System.out.println ("A partridge in a pear tree");
}
}
}
}```

This is my code and I get an error message saying that the "switch (x)" is not a compatible type and it is expecting an int variable.
Not sure where to go from here

9. ## Re: Implementing the switch statement

expecting an int variable.
Make the switch variable an int. All the case statements are int values. Get rid of the x variable.

10. ## Re: Implementing the switch statement

so completely take out the string = x; and delete the switch (x) but replace it with just int?

11. ## Re: Implementing the switch statement

What happened when you used an int value there instead a String?