Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Java Newbie...just stuck on this

  • 08-11-2013 12:33pm
    #1
    Registered Users Posts: 23


    Hey folks,
    I'm a Java newbie ..am just 8 weeks into Java 7 Programming 1...I'm trying to get a bit of code working that displays the lyrics to the 12 days of Cantona song...

    This is what I have so far..

    public class CantonaSong {

    public static void main(String[] args) {
    String[] xmasDays = { "second", "third", "fourth", "fifth", "sixth",
    "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" };
    String fullname = " an Eric Cantona";
    String name = " Cantona's";
    int count = 2;

    System.out
    .println("On the first day of Christmas, my true love gave to me,"
    + fullname);
    for (String x : xmasDays) {

    if (count == 5) {
    System.out
    .println("On the "
    + x
    + " day of Christmas, my true love gave to me, FIIIIIIVVVEEE"
    + name + "," + name + ", and" + fullname);
    }

    else {

    System.out.println("On the " + x
    + " day of Christmas, my true love gave to me, "
    + count + "" + name + "," +(count-1)+ name + ", and" + fullname);
    }

    count++;

    }

    }
    }


    I'm a little stuck...if should print out after the 4rth day of christmas " 4 Cantona's, 3 Cantona's, 2 Cantona, and an Eric Cantona"..and the same all the way up..." 12 Cantona's, 11, 10 etc"

    What am I missing?
    Tagged:


Comments

  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,082 Mod ✭✭✭✭Tar.Aldarion


    Use
    tags to format your post better. Even though it appears to be pretty bad
    
    What is the output you are receiving? Is there an error?
    Seems to me you are jsut hardcoding teh amount of times it will run so it will always give you a amax of 3 eric cantanas
    
    [code]+ count + "" + name + "," +(count-1)+ name + ", and" + fullname);
    

    Whereas you should design it to increment and print for all necessary times.
    Why is there this count==5 business that will just be run once, while each other time the else will run.


  • Registered Users Posts: 23 mickells35


    Thanks for replying...I'm getting no error but am not getting the output I require..
    Here is my output

    On the first day of Christmas, my true love gave to me, an Eric Cantona
    On the second day of Christmas, my true love gave to me, 2 Cantona's, 1 Cantona's, and an Eric Cantona
    On the third day of Christmas, my true love gave to me, 3 Cantona's, 2 Cantona's, and an Eric Cantona
    On the fourth day of Christmas, my true love gave to me, 4 Cantona's, 3 Cantona's, and an Eric Cantona
    On the fifth day of Christmas, my true love gave to me, FIIIIIIVVVEEE Cantona's, Cantona's, and an Eric Cantona
    On the sixth day of Christmas, my true love gave to me, 6 Cantona's, 5 Cantona's, and an Eric Cantona
    On the seventh day of Christmas, my true love gave to me, 7 Cantona's, 6 Cantona's, and an Eric Cantona
    On the eighth day of Christmas, my true love gave to me, 8 Cantona's, 7 Cantona's, and an Eric Cantona
    On the ninth day of Christmas, my true love gave to me, 9 Cantona's, 8 Cantona's, and an Eric Cantona
    On the tenth day of Christmas, my true love gave to me, 10 Cantona's, 9 Cantona's, and an Eric Cantona
    On the eleventh day of Christmas, my true love gave to me, 11 Cantona's, 10 Cantona's, and an Eric Cantona
    On the twelfth day of Christmas, my true love gave to me, 12 Cantona's, 11 Cantona's, and an Eric Cantona

    I need to add each previous result to the current one..e.g the last line should output:

    On the twelfth day of Christmas, my true love gave to me, 12 Cantona's, 11 Cantona's, 10 Cantona's, 9 Cantona's etc...and an Eric Cantona


  • Registered Users Posts: 2,089 ✭✭✭henryporter


    Have you looked at using a switch statement?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,082 Mod ✭✭✭✭Tar.Aldarion


    So if I take it correctly you want to print the first result and append it to the second result and so on. What you have to do is sit down and thing about the logic and flow that is needed to do this. Eg, you make the first string, now you store it, then on the second iteration what do you need to print, and where does your stored sting come in and so on.


    I note that 5 is a special case but you can reduce that code by removing the else:

    fiveCase ="";
    
    //for loop etc
    if (count == 5)
        {
            fiveCase ="FIIIIIIVVVEEE";
        }
    System.out.println("On the "+ x+ " day of Christmas, my true love gave to me, [b]+fiveCase[/b] "+ name + "," + name + ", and" + fullname);
    
    //end of current iteration
    fiveCase="";
    

    This will mean that fiveCase will add nothing unless the count is actually 5.


  • Registered Users Posts: 23 mickells35


    switch would work but that would mean I'm coding the print statement 12 times...I want to code it as little as possible..I probably have to manipulate the count variable again somehow..


  • Advertisement
  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Use some kind of recursion. Did you cover this yet in course?

    Make a simple function printCantona(int howmany), which prints the Nth bit, then calls printCantona( howmany - 1). Need some special handling for N = 1 and N = 5, otherwise do generic printout.


  • Registered Users Posts: 7,859 ✭✭✭The_B_Man


    Here's a very rough idea of what you need. First you need another loop.

    You're hardcoding in how many Cantonas will be output in the System.out.println().

    If I understand correctly, you need to perhaps store the "FIIIIIIIIIIVE" or "count" in a variable first. So with your if statement, have like "if count ==5, myString = "FIIIIVE", else myString = count"

    then after, have a system.out.println, have "On the +x+ day of xmas, my true love gave to me, +myString+ cantonas,"
    then after that, have a loop that goes "while count-- >1, print count + " cantonas,"
    then after that. just print "and an Eric Cantona".

    You'll have to refactor how you use your count variable though. Reset it to xmasDays.length on each loop of your for loop.


  • Registered Users Posts: 23 mickells35


    The uppercase FIIVVEE is there just because thats the way the fans sing it at Old Trafford ...dont worry about it guys..its just a little thing I was messing around with with the intention that I might learn something...I think an inner loop might do it..
    either the higher up I go, I'm always always adding a Cantona..thus decrementing the count variable an extar time for each print statement

    Thanks


  • Registered Users Posts: 6,999 ✭✭✭witnessmenow


    Build up your string using a for loop, and when you are finished println it

    I havent tested this but the general idea should be ok, put this inside your current for loop, keep the count ++.

    When you get this working as you want had a case for the FIIVEEEE
    
    string outputText = "";
    
    outputText  = "On the " + x + " day of Christmas, my true love gave to me, ";
    
    for(i=count; i > 1; i--)
    {
        outputText  = outputText  + i + "" + name + ", ";
    }
    
    outputText  = outputText  + "and" + fullname;
    
    
    System.out.println(outputText )
    
    
    
    


  • Registered Users Posts: 342 ✭✭adm


    This should work.
    public class CantonaSong {
    
    	public static void main(String[] args) {
    		String[] xmasDays = { "first", "second", "third", "fourth", "fifth",
    				"sixth", "seventh", "eighth", "ninth", "tenth", "eleventh",
    				"twelfth" };
    
    		int count = 0;
    		String cantonas, printLine;
    
    		for (String day : xmasDays) {
    
    			printLine = "On the " + day
    					+ " day of xmas my true love gave to me ";
    
    			if (count > 0) {
    				for (int i = 0; i < count; i++) {
    
    					// ternary just for succinctness
    					cantonas = (count - (i - 1)) == 5 ? "FIIIIIIVVVEEE"
    							: Integer.toString(count - (i - 1));
    
    					printLine += cantonas + " Cantona's";
    
    					// trailing comma for all except last item in loop
    					if (i + 1 != count) {
    						printLine += ",";
    					}
    
    					printLine += " ";
    
    				}
    
    				printLine += "and ";
    			}
    			printLine += "an Eric Cantona.";
    
    			System.out.println(printLine);
    
    			count++;
    
    		}
    	}
    }
    


  • Advertisement
  • Registered Users Posts: 23 mickells35


    To adm...thats great...thanks very much.


Advertisement