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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Vacation Switch

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Vacation Switch

    These are my program requrements:

    1. Create a program that asks the user to choose a number from 1 - 5.
    2. Using a switch statement, assign a vacation package to each number.
    3. Display a congratulations message which includes the vacation package information.
    4. Display a message to prompt the user to enter the first name, last name, and age of four guests using a for loop which will create a list of the four guests.
    5. Prompt the user to enter a departure date.
    6. Display the congratulations message, guest list, and the departure date.

    I have been able to do steps 1 - 4 but have been unable to correctly program steps 5 - 6. The problems I am having are listed in order below.

    5. I can't figure out how to get the entire departure date to print out i.e. "July 1, 2013". Instead it only prints out "July".
    6. I can't figure out how to get the congratulations message from the switch statement to be displayed a second time at the end of my code.

    So far my code is as follows:

     
    import java.util.Scanner;
     
    public class MidTerm
    {
      public static void main(String[]args)
      {
        Scanner keyboard = new Scanner(System.in);
     
        String vacationNumber;
        int count;
        String guest;
        String fName;
        String lName;
        String age;
        String list = "";
        String date;
     
    //*************************************************
        System.out.print("Enter a number between 1 - 5 ");
        vacationNumber = keyboard.nextLine();
     
        switch (vacationNumber.charAt (0))
        {
        case  '1':
        System.out.println("Congragulations! You won a three day Cruise to the Bahamas!");
        break;
        case  '2':
        System.out.println("Congragulations! You won a three day Cruise to the Caribbean!");
        break;
        case  '3':
        System.out.println("Congragulations! You won a three day trip to New York City!");
        break;
        case  '4':
        System.out.println("Congragulations! You won a three day trip to Hawaii!");
        break;
        case  '5':
        System.out.println("Sorry! You won a four month trip to Afghanistan!");
        break;
        }
        for(count = 1; count <= 4; count++)
        {
          System.out.print("Enter the first name of guest # " + count +":");
          fName = keyboard.next();
          list = list + fName + "\t";
          System.out.println();
     
          System.out.print("Enter the last name of guest # " + count +":");
          lName = keyboard.next();
          list = list + lName + "\t";
          System.out.println();
     
          System.out.print("Enter the age of guest # " + count +":");
          age = keyboard.next();
          list = list + age + "\n";
          System.out.println();
        }
        System.out.print("Please enter a departure date");
        date = keyboard.next();
     
        System.out.print(list + "\n");
        System.out.print(date);
      }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Vacation Switch

    Quote Originally Posted by Program83 View Post
    5. I can't figure out how to get the entire departure date to print out i.e. "July 1, 2013". Instead it only prints out "July".
    This part of the code seems to be missing. Be sure to include the code exactly as it is during testing.

    Quote Originally Posted by Program83 View Post
    6. I can't figure out how to get the congratulations message from the switch statement to be displayed a second time at the end of my code.
    You will have to store the message in a variable with a large enough scope to cover both uses of it. Search keywords java and scope to find more details.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Vacation Switch

    Quote Originally Posted by Program83 View Post
    6. I can't figure out how to get the congratulations message from the switch statement to be displayed a second time at the end of my code.
    That one's actually really easy. Simply use an Array or an ArrayList to store the congratulations messages.

    Example Code:
    ArrayList<String> congratulationsMessages = new ArrayList<String>();
     
    congratulationsMessages.add("Congratulations! You won a three day Cruise to the Bahamas!");
    congratulationsMessages.add("Congratulations! You won a three day Cruise to the Caribbean!");
    congratulationsMessages.add("Congratulations! You won a three day trip to New York City!");
    congratulationsMessages.add("Congratulations! You won a three day trip to Hawaii!");
    congratulationsMessages.add("Sorry! You won a four month trip to Afghanistan!");

    Then, you can use the same variable you used in the switch statement like this:
    System.out.println(congratulationsMessages.get(vacationNumber.charAt (0) -1));

    That should work. You might also be able to replace the whole switch statement with this one line, actually. You might as well give it a shot.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

Similar Threads

  1. [SOLVED] Switch
    By KillerToFu in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 21st, 2013, 05:53 PM
  2. what switch?
    By kingzeze in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 26th, 2013, 11:09 AM
  3. Switch
    By hiepa in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 19th, 2012, 06:21 PM
  4. Creative vacation project idea?
    By MGhareeb in forum The Cafe
    Replies: 14
    Last Post: January 25th, 2011, 08:29 AM