# Help with Homework Problem

• 10-05-2010, 03:44 AM
Holy_Zombies
Help with Homework Problem
So I am running into a couple problems.

Here's part of my specs:

Now, you are to first prompt the user for his/her name and their classification (numeric) (1 = Freshman, 2 = Sophomore, …, 5 Graduate Student). You are to then print out his/her name and classification as follows:

This output is produced by Joe Schmuck (please use your name), who is a junior at UNF.

Note, JoeSchmuck is a String input and requires no conversion. ‘3’ is also a character that is inputted, but your Scanner object will take care of this for you.

Once this line is printed on the screen, you are to skip two lines (two blank lines). Then you are to prompt the user (me, again) for a number between 1 and 12.

Bad inputs: If the number the user submits does not lie between 1 and 12, you are to prompt the user to try again, such as providing the message, “Try Again. Enter a number between 1 and 12 inclusive and press Enter” and allow the misguided person (probably me again!) to re-enter another number. (This will require a loop). If the user fails to submit a number between 1 and 12 with three tries (total, including the first attempt and two repeats) then you are to display a message, “Get a Life! Program Terminated” and terminate your program. (Be certain to test this option. I will.)

So far I have most of the code done, However I'm not sure how to receive the users name and class # and put together.

Also I can't figure out how to get the part "Get A life..etc." to show before the program terminates.
• 10-05-2010, 03:45 AM
Holy_Zombies
Here is my code so far:

Code:

```package project2; import java.util.Scanner; /**  *  * @author at  */ public class Main {     /**     * @param args the command line arguments     */     public static void main(String[] args)     {       Scanner keyboard = new Scanner(System.in); int day = 0; int count = 0; while ((day < 1 || day > 12) && count < 3)     { //start while         count++;             System.out.print("Enter number: ");             day = keyboard.nextInt();     if (day > 12 || day < 1)         {//start if           System.out.print("Please Try Again \n");           }//end if     else           {//start else             System.out.print("On the ");             switch(day)           {             case 1:                 System.out.print("1st");                 break;             case 2:                 System.out.print("2nd");                 break;             case 3:                 System.out.print("3rd");                 break;             default:                 System.out.print(day+"th");           }    System.out.println(" day of Christmas my true love gave to me,");         switch(day) { //start switch             case 12:                 System.out.println("Twelve Drummers Drumming");             case 11:                 System.out.println("Eleven Pipers Piping" );             case 10:                 System.out.println("Ten Lords A-leaping" );             case 9:                 System.out.println("Nine Ladies Dancing" );             case 8:                 System.out.println("Eight Maids A-milking" );             case 7:                 System.out.println("Seven Swans A-swimming");             case 6:                 System.out.println("Six Geese A-laying" );             case 5:                 System.out.println("Five Golden Rings" );             case 4:                 System.out.println("Four Calling Birds" );             case 3:                 System.out.println("Three French Hens" );             case 2:                 System.out.println("Two Turtle Doves, and" );             case 1:                 System.out.println("A partridge in a pear tree"); }  //end switch     }//end else     }//end while     }//end main     }//end class```
• 10-05-2010, 03:48 AM
Fubarable
Quote:

Originally Posted by Holy_Zombies
So far I have most of the code done, However I'm not sure how to receive the users name and class # and put together.

edit: the Scanner object, keyboard, will have methods that you can use to get user input. One, nextLine() will get the next line that the user enters. There's another one that I'll let you look up for getting int input (the number 3 for instance).

Quote:

Also I can't figure out how to get the part "Get A life..etc." to show before the program terminates.
Hello and welcome to the java-forums!

Let's see what you've tried so far in your attempt to solve these two problems above and also let us know just how you're stuck or what isn't working. The more we know the better we'll be able to help you. Also, if posting code, please read my first signature link on how to use code tags so that your code will retain its formatting and be readable.

Best of luck!

edit: I now see your code post and have taken the liberty of adding code tags, but if you pardon my being blunt, your formatting is bad and even with code tags your code is very hard to read. It is in your best interest and ours for you to correct the mal-indentation so that it conforms to standards. If your indentation is proper, then your code won't need all those distracting comments such as // start of if and // end of if... So get rid of them.

To do your little statement if count is > 3, use your if block that you already have in place.

Again, luck!
• 10-05-2010, 03:59 AM
Holy_Zombies
those //start things are just for me to keep track of things right now. im VERY new to java.

So how to I add a condition to the if for count >3 ? does it go with the day<1 >12 part?
• 10-05-2010, 04:23 AM
Holy_Zombies
okay I made a little progress. Now it will loop 3 times and kick you with the correct message.

Code:

``` package project2aaronturner; import java.util.Scanner; /**  *  *  */ public class Main {     /**     * @param args the command line arguments     */     public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Scanner input = new Scanner( System.in ); int day = 0; int count = 0;             System.out.print("Enter name: ");           String theName = input.nextLine();           // System.out.print("Enter year level: ");           // ENTER YEAR LEVEL           System.out.println(theName); // DISPLAY NAME + YEAR LEVEL while ((day < 1 || day > 12) && count < 3)     { //start while         count++;             System.out.print("Enter number: ");             day = keyboard.nextInt();             if ((day < 1 || day > 12) && count < 3)     {//start if (day < 12) && count < 3         System.out.print("Try Again.  Enter a number between 1 and 12 inclusive"                         + " and press Enter \n\n");     }//end if (day < 12) && count < 3             else if (day > 12)             {                 System.out.print("Get a Life! Program Terminated \n\n");             }     else     {//start else (day >= 12)             System.out.print("On the ");         switch(day)         {             case 1:                 System.out.print("1st");                 break;             case 2:                 System.out.print("2nd");                 break;             case 3:                 System.out.print("3rd");                 break;             default:                 System.out.print(day+"th"); } System.out.println(" day of Christmas my true love gave to me,");         switch(day) { //start switch             case 12:                 System.out.println("Twelve Drummers Drumming");             case 11:                 System.out.println("Eleven Pipers Piping" );             case 10:                 System.out.println("Ten Lords A-leaping" );             case 9:                 System.out.println("Nine Ladies Dancing" );             case 8:                 System.out.println("Eight Maids A-milking" );             case 7:                 System.out.println("Seven Swans A-swimming");             case 6:                 System.out.println("Six Geese A-laying" );             case 5:                 System.out.println("Five Golden Rings" );             case 4:                 System.out.println("Four Calling Birds" );             case 3:                 System.out.println("Three French Hens" );             case 2:                 System.out.println("Two Turtle Doves, and" );             case 1:                 System.out.println("A partridge in a pear tree"); }  //end switch     }//end else (day >= 12)     }//end while     }//     }```
Still cant get the name part right.
• 10-05-2010, 04:59 AM
Fubarable
1) you need just one Scanner object, not two. You just need to learn to use it. Again, use the method I gave you above plus look up the other method. Also check out the tutorial here: Scanning