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 4 of 4

Thread: Java Program Help Switch Statement

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Program Help Switch Statement

    Ok so I got this homework problem that asks to get the month and the date of the user and then ask them to enter a year for which they would like to know the day of the week on which their birthday falls. The program should display the day of the week for your birthday of that year. I need to use a switch statement to determine the day of the week instead of just displaying (1,2,3,4) like it does by default. All I need some help on is getting it display the Day instead of the number. Can somebody help me out

    This is what I got
    import java.util.Scanner;
    import java.util.GregorianCalendar;
    import javax.swing.JOptionPane;            // Import JOptionPane from the swing class
    import java.text.DateFormat;//Import Scanner
     
    public class MyBirthdays
     
    {
    //main method
     
        public static void main(String[] args)        //Main Method
        {
     
            // Declare and initialize variables
     
     
            String monthString, dayString,yearString;
     
            int month,day,year;
     
            monthString    =JOptionPane.showInputDialog("Enter Month You were born in." );
            month = Integer.parseInt(monthString);                                    // Convert string into integer and stores the value  in age
     
            dayString = JOptionPane.showInputDialog("What's the day u were born on ");    // Ask user for name of month
            day = Integer.parseInt(dayString);        // Convert string dayString into integer and store value in day.
     
            yearString = JOptionPane.showInputDialog("What's the year u were born on ");            // Ask user for numerical day born on
            year = Integer.parseInt(yearString);        // Convert string dayString into integer and store value in day.
     
            month-=1;
            GregorianCalendar birthYear = new GregorianCalendar(year,month,day);
            int birth = birthYear.get(GregorianCalendar.DAY_OF_WEEK);
     
            System.out.println( birth);
            //switch (month)
     
     
            {
                //case 1: month = "January";
                //break;
                /*case 2: month = "February"
                case 3: month = "March"
                case 4: month = "April"
                case 5: month = "May"
                case 6: month = "June"*/
            }
            GregorianCalendar birthdate = new GregorianCalendar(year,month,day);
            long birthTime = birthdate.getTimeInMillis();
            DateFormat longDate = DateFormat.getDateInstance( DateFormat.LONG);
            String birthDateString = longDate.format(birthTime);
            System.out.println(birthDateString);
     
            //JOptionPane.showMessageDialog( null, outputMessage );
     
     
    }
     
    }


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Java Program Help Switch Statement

    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Note which data types are valid for switch/case statements. String isn't one of them.

    db

  3. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    javapenguin (November 10th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Java Program Help Switch Statement

    I thought that String would surely be a data type you could use in a switch statement.

  5. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Java Program Help Switch Statement

    There was an RFE to make that possible in Java 7, but I don't think it made it.

    db

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. Advancing day w/ if/else & switch statement
    By rbread80 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 10:43 PM
  3. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  4. How to populate a switch statement with a linked list
    By macnasty in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 10:47 PM
  5. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM