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:o
This is what I got
Code :
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 );
}
}
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
Re: Java Program Help Switch Statement
I thought that String would surely be a data type you could use in a switch statement.
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