Results 1 to 7 of 7
  1. #1
    Holy_Zombies is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    Holy_Zombies is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Here is my code so far:

    Java 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
    Last edited by Holy_Zombies; 10-05-2010 at 03:54 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Holy_Zombies View Post
    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).

    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!
    Last edited by Fubarable; 10-05-2010 at 03:56 AM.

  4. #4
    Holy_Zombies is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    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?
    Last edited by Holy_Zombies; 10-05-2010 at 04:02 AM.

  5. #5
    Holy_Zombies is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    okay I made a little progress. Now it will loop 3 times and kick you with the correct message.

    Java 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    2) Again, please please please correct your indentation as your code formatting is atrocious. If you want to encourage folks to read your code and help you, you should make that code easy to read.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    If you want to see how indentation should be done "properly," have a look at this. It will help you organize your code in such a way that it is readable by everyone, and you will NEVER have to guess whether or not a specific line of code is with a particular if/while/for/etc. block--you will just know.

Similar Threads

  1. Homework help please
    By chick in forum New To Java
    Replies: 22
    Last Post: 03-19-2010, 07:39 AM
  2. Replies: 6
    Last Post: 02-20-2010, 03:27 AM
  3. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 03:37 PM
  4. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 PM
  5. Homework PREREQUISITE Problem
    By ChrisC in forum New To Java
    Replies: 7
    Last Post: 11-27-2007, 05:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •