d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Problem > Can You Help Me Out?
12Next
Add Reply New Topic New Poll
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Mar 18 2014 10:54pm
Hi. I am having problems trying to write the lyrics to "The Twelve Days of Christmas".
It is supposed to consist of of a counter-controlled repetition statement along with 2 switch statements.

The first switch statement is done correctly I believe so you can skip to the 2nd switch statement ( // determine what was sent (2nd half )
The cases ARE supposed to go from 12 to 1 but for some reason the coding is printing out the two system.out.println()'s I have written down in between the 11th and 12th day.

The program is just supposed to print out the lyrics to the song in this fashion:


Heres my coding:
Code
public class Assign5
{
public static void main(String[] args)
{

// loop-continuation condition and increment
for ( int day = 1; day <= 12; day++)
{
String strDay = "";

// determine which day (first sentence)
switch (day)
{

// First day
case 1:
strDay = "first";
break;
// Second day
case 2:
strDay = "second";
break;
// Third day
case 3:
strDay = "third";
break;
// Fourth day
case 4:
strDay = "fourth";
break;
// Fifth day
case 5:
strDay = "fifth";
break;
// Sixth day
case 6:
strDay = "sixth";
break;
// Seventh day
case 7:
strDay = "seventh";
break;
// Eighth day
case 8:
strDay = "eighth";
break;
// Ninth day
case 9:
strDay = "ninth";
break;
// Tenth day
case 10:
strDay = "tenth";
break;
// Eleventh day
case 11:
strDay = "eleventh";
break;
// Twelfth day
case 12:
strDay = "twelfth";
break;

}

// determine what was sent ( 2nd half )
switch (day)
{
// First day
case 12:
System.out.println("A Partridge in a Pear Tree\n");
// Second day
case 11:
System.out.println("Two Turtle Doves\nand a Partridge in a Pear Tree\n");

}
// Display the day of Christmas
System.out.printf( "On the %s day of Christmas\n my true love sent to me:\n",
strDay);
}


}

}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 18 2014 11:24pm
Code

String song = "";

switch(day)
{
case 12: song += "Twelve Drummers Drumming\n";
case 11: song += "Eleven Pipers Pipping\n";
case 10: song += "Ten Lords-a-Leaping\n";
case 9: song += "Nine Ladies Dancing\n";
case 8: song += "Eight Maids-a-milking\n";
case 7: song += "Swans-a-swimming\n";
case 6: song += "Geese-a-laying\n";
case 5: song += "Five Golden Rings\n";
case 4: song += "Four Calling Birds\n";
case 3: song += "Three French Hens\n";
case 2: song += "Two Turtle Doves\nand ";
case 1: song += "A Partridge in a Pair Tree.";
}
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Mar 19 2014 12:23am
Thanks for the help. I know what I sent isn't much but it's all I have. If you can also show me how to do a little bit more, that would be great.

1) The first day should say "A Partridge in a Pear Tree" while the other days should say "and A Partridge in a Pear Tree"
I am supposed to use conditional operator ("OR" ||) I believe.
How do I implement this?

2) At the moment its run in cmd. How do I run it in a dialog box with a scroll bar?

Hint: Use the following code to create a JTextArea and a JScrollPane which are imported from javax.swing

JTextArea songArea = new JTextArea(20, 30);
JScrollPane scroller = new JScrollPane(songArea);
songArea.setText(songString);
JOptionPane.showMessageDialog(null, scroller, "Twelve Days of Christmas",
JOptionPane.PLAIN_MESSAGE);

Should look like this:


and heres my up-to-date coding (TextArea/ScrollPane already imported but that is all):
Code
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class Assign5
{
public static void main(String[] args)
{
// Display Title
System.out.print(" Twelve Days of Christmas\n");

// loop-continuation condition and increment
for ( int day = 1; day <= 12; day++)
{
String strDay = "";

// determine which day (first sentence)
switch (day)
{

// First day
case 1:
strDay = "first";
break;
// Second day
case 2:
strDay = "second";
break;
// Third day
case 3:
strDay = "third";
break;
// Fourth day
case 4:
strDay = "fourth";
break;
// Fifth day
case 5:
strDay = "fifth";
break;
// Sixth day
case 6:
strDay = "sixth";
break;
// Seventh day
case 7:
strDay = "seventh";
break;
// Eighth day
case 8:
strDay = "eighth";
break;
// Ninth day
case 9:
strDay = "ninth";
break;
// Tenth day
case 10:
strDay = "tenth";
break;
// Eleventh day
case 11:
strDay = "eleventh";
break;
// Twelfth day
case 12:
strDay = "twelfth";
break;

}

String strSong = "";

// determine what was sent ( 2nd half )
switch (day)
{
// Twelfth day
case 12:
strSong += " Twelve Drummers Drumming\n";
// Eleventh day
case 11:
strSong += " Eleven Pipes Piping\n";
// Tenth day
case 10:
strSong += " Ten Lords a Leaping\n";
// Ninth day
case 9:
strSong += " Nine Ladies Dancing\n";
// Eighth day
case 8:
strSong += " Eight Maides a Milking\n";
// Seventh day
case 7:
strSong += " Seven Swans a Swiming\n";
// Sixth day
case 6:
strSong += " Six Geese a Laying\n";
// Fifth day
case 5:
strSong += " Five Golden Rings\n";
// Fourth day
case 4:
strSong += " Four Calling Birds\n";
// Third day
case 3:
strSong += " Three French Hens\n";
// Second day
case 2:
strSong += " Two Turtle Doves\n";
// First day
case 1:
strSong += " and a Partridge in a Pear Tree";

}

// Display the day of Christmas
System.out.printf( "\nOn the %s day of Christmas\n my true love sent to me:\n",
strDay);
// Display the song
System.out.printf( "%s\n", strSong);
}


}

}


This post was edited by Modification on Mar 19 2014 12:42am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 19 2014 07:06am
the way i originally had it handled the case for "and", to handle the lower/upper case "a" for day 1, you could just change the first character of strSong to an uppercase after you exit the switch statement.
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Mar 19 2014 05:19pm
Quote (Minkomonster @ Mar 19 2014 06:06am)
the way i originally had it handled the case for "and", to handle the lower/upper case "a" for day 1, you could just change the first character of strSong to an uppercase after you exit the switch statement.


Ah OK I got it. What about the window+scroll?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 19 2014 07:14pm
Something like this?

Code
import javax.swing.*;


public class Test {
public static void main(String[] args)
{
String songString = "";
for(int day = 1; day <= 12; day++)
{
songString += String.format("On the %s day of Christmas\nmy true love gave to me:\n%s\n\n",getDay(day),getSong(day));
}

JTextArea songArea = new JTextArea(20, 30);
JScrollPane scroller = new JScrollPane(songArea);
songArea.setText(songString);
JOptionPane.showMessageDialog(null, scroller, "Twelve Days of Christmas",
JOptionPane.PLAIN_MESSAGE);
}

private static String getDay(int day)
{
switch(day)
{
case 1: return "first";
case 2: return "second";
case 3: return "third";
case 4: return "forth";
case 5: return "fifth";
case 6: return "sixth";
case 7: return "seventh";
case 8: return "eigth";
case 9: return "ninth";
case 10: return "tenth";
case 11: return "eleventh";
case 12: return "twelfth";
default: return "";

}

}

private static String getSong(int day)
{
String song = "";

switch(day)
{
case 12: song += "Twelve Drummers Drumming\n";
case 11: song += "Eleven Pipers Pipping\n";
case 10: song += "Ten Lords-a-Leaping\n";
case 9: song += "Nine Ladies Dancing\n";
case 8: song += "Eight Maids-a-milking\n";
case 7: song += "Seven Swans-a-swimming\n";
case 6: song += "Six Geese-a-laying\n";
case 5: song += "Five Golden Rings\n";
case 4: song += "Four Calling Birds\n";
case 3: song += "Three French Hens\n";
case 2: song += "Two Turtle Doves\nand ";
case 1: song += "A Partridge in a Pair Tree.";
}

return song;
}
}
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Mar 19 2014 11:52pm
brilliant. I only have a small problem with it.

The indentation is not working should be like this:



but mine is looking like this:



heres the code:

Code
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class Assign5
{

public static void main(String[] args)
{
String songString = "";

String titleString = " Twelve Days of Christmas\n\n";

// print song
for(int day = 1; day <= 12; day++)
{
songString += String.format( titleString +
"On the %s day of Christmas\n"
+ " my true love gave to me:\n"
+ " %s\n\n",getDay(day),getSong(day));
}

JTextArea songArea = new JTextArea(20, 30);
JScrollPane scroller = new JScrollPane (songArea);
songArea.setText(songString);
JOptionPane.showMessageDialog(null, scroller, "Twelve Days of Christmas",
JOptionPane.PLAIN_MESSAGE);
}
// create getDay
private static String getDay(int day)
{
String strDay = "";

// determine which day (first sentence)
switch (day)
{

// First day
case 1:
strDay = "first";
break;
// Second day
case 2:
strDay = "second";
break;
// Third day
case 3:
strDay = "third";
break;
// Fourth day
case 4:
strDay = "fourth";
break;
// Fifth day
case 5:
strDay = "fifth";
break;
// Sixth day
case 6:
strDay = "sixth";
break;
// Seventh day
case 7:
strDay = "seventh";
break;
// Eighth day
case 8:
strDay = "eighth";
break;
// Ninth day
case 9:
strDay = "ninth";
break;
// Tenth day
case 10:
strDay = "tenth";
break;
// Eleventh day
case 11:
strDay = "eleventh";
break;
// Twelfth day
case 12:
strDay = "twelfth";
break;
}
return strDay;
}

// create strSong
private static String getSong(int day)
{
String strSong = "";

// determine what was sent ( 2nd half )
switch (day)
{
// Twelfth day
case 12:
strSong += "Twelve Drummers Drumming\n";
// Eleventh day
case 11:
strSong += "Eleven Pipes Piping\n";
// Tenth day
case 10:
strSong += "Ten Lords a Leaping\n";
// Ninth day
case 9:
strSong += "Nine Ladies Dancing\n";
// Eighth day
case 8:
strSong += "Eight Maides a Milking\n";
// Seventh day
case 7:
strSong += "Seven Swans a Swiming\n";
// Sixth day
case 6:
strSong += "Six Geese a Laying\n";
// Fifth day
case 5:
strSong += "Five Golden Rings\n";
// Fourth day
case 4:
strSong += "Four Calling Birds\n";
// Third day
case 3:
strSong += "Three French Hens\n";
// Second day
case 2:
strSong += "Two Turtle Doves\n";
// First day
case 1:
strSong += (day > 1 ? "and a Partridge in a Pear Tree" : "A Partridge in a Pear Tree");

}

return strSong;
}




}


Appreciate it if you can help me out with this last one.

This post was edited by Modification on Mar 20 2014 12:15am
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Mar 20 2014 07:29pm
well i figured out the spacing but the title (Twelve Days of Christmas) on the very first line is not showing up (as the picture suggests)
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 20 2014 07:36pm
dont add the title every iteration. initialize songString to be the title, and remove titleString all together.
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Mar 20 2014 07:49pm
Quote (Minkomonster @ Mar 20 2014 06:36pm)
dont add the title every iteration. initialize songString to be the title, and remove titleString all together.


perfected. thanks so much minko!
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll