•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,384 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,986 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 32805 | Replies: 2
![]() |
•
•
Join Date: Jul 2004
Posts: 16
Reputation:
Rep Power: 5
Solved Threads: 0
hi everyone,
i am very new in java. i am having problem in SWITCH CASE STATEMENT. can anyone please help me to solve the following problem:
you have to write a program in java to generate the following menu:
1: DATE
2: MONTH
3: YEAR
4: DAY
if the choice is 1 print date, if 2 print month, if 3 print year, if 4 print year. Do the program using switch case statement
thank you,
regards,
shantuli
i am very new in java. i am having problem in SWITCH CASE STATEMENT. can anyone please help me to solve the following problem:
you have to write a program in java to generate the following menu:
1: DATE
2: MONTH
3: YEAR
4: DAY
if the choice is 1 print date, if 2 print month, if 3 print year, if 4 print year. Do the program using switch case statement
thank you,
regards,
shantuli
•
•
Join Date: Jul 2004
Location: Manchester UK
Posts: 16
Reputation:
Rep Power: 5
Solved Threads: 0
I hope this helps, the main stages are as follows:
1. Read In a String literal
2. Create a Calender object
3. Set up arrays for day names and month names
4. Get the current values of the date using the Calender.get(XYZ) methods
5. Based on these values get the correct names from the arrays - i.e. monday
/ july
6. Continously Display a menu using the do-while loop
7. Prompt user for number, convert the string(choiceString) to an
integer(choice)
8. Set a task for each integer using the Switch statement
9. If integer = 5, then exit.
-------------------------------------------------------------------------------
import java.io.*;
import java.io.IOException;
import java.util.*;
public class Test
{
public static void main(String[] args) throws IOException
{
//create a bufferedReader object
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
String choiceString;
int choice;
//create a Calender object
Calendar c = Calendar.getInstance();
//create an array to hold day names
//these cant be changed later therefore are declared final.
final String dayNames[] =
{"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
//create an array to hold month names
//these cant be changed later therefore are declared final.
final String monthNames[] =
{"January",
"February",
"march",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"Novemeber",
"December"};
//java notation for MONTH is 0-11, where 0 = January
int month = (c.get(Calendar.MONTH) + 1);
//java notation for DAY_OF_WEEK is 0-6, where 0 = Sunday
int day = (c.get(Calendar.DAY_OF_WEEK) -1);
//variable to hold day names
String dayString = dayNames[day-1];
//variable to hold month names
String monthString = monthNames[month-1];
//consistantly ask for user input
do
{
//print out a menu
System.out.println("\n------- Menu----------");
System.out.println("1. DATE");
System.out.println("2. MONTH");
System.out.println("3. YEAR");
System.out.println("4. DAY");
System.out.println("5. Quit");
System.out.println("Enter choice [1,2,3,4,5]: ");
System.out.flush();
choiceString = in.readLine();
//convert string to integer
choice = Integer.parseInt(choiceString);
switch (choice)
{
//the choices go here - print the details
case 1:
System.out.println("\nDATE: " + c.get(Calendar.DATE) ); break;
case 2:
System.out.println("\nMONTH: " + monthString); break;
case 3:
System.out.println("\nYEAR: " + c.get(Calendar.YEAR)); break;
case 4:
System.out.println("\nDAY: " + dayString); break;
case 5: //exit
System.exit(0);
default:
System.out.println("\nPlease choose a number between 1-5 only
\n");
}
}
while(choice!= 5);
}
}
--------------------------------------------------------
Hope that helps! rickster
1. Read In a String literal
2. Create a Calender object
3. Set up arrays for day names and month names
4. Get the current values of the date using the Calender.get(XYZ) methods
5. Based on these values get the correct names from the arrays - i.e. monday
/ july
6. Continously Display a menu using the do-while loop
7. Prompt user for number, convert the string(choiceString) to an
integer(choice)
8. Set a task for each integer using the Switch statement
9. If integer = 5, then exit.
-------------------------------------------------------------------------------
import java.io.*;
import java.io.IOException;
import java.util.*;
public class Test
{
public static void main(String[] args) throws IOException
{
//create a bufferedReader object
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
String choiceString;
int choice;
//create a Calender object
Calendar c = Calendar.getInstance();
//create an array to hold day names
//these cant be changed later therefore are declared final.
final String dayNames[] =
{"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
//create an array to hold month names
//these cant be changed later therefore are declared final.
final String monthNames[] =
{"January",
"February",
"march",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"Novemeber",
"December"};
//java notation for MONTH is 0-11, where 0 = January
int month = (c.get(Calendar.MONTH) + 1);
//java notation for DAY_OF_WEEK is 0-6, where 0 = Sunday
int day = (c.get(Calendar.DAY_OF_WEEK) -1);
//variable to hold day names
String dayString = dayNames[day-1];
//variable to hold month names
String monthString = monthNames[month-1];
//consistantly ask for user input
do
{
//print out a menu
System.out.println("\n------- Menu----------");
System.out.println("1. DATE");
System.out.println("2. MONTH");
System.out.println("3. YEAR");
System.out.println("4. DAY");
System.out.println("5. Quit");
System.out.println("Enter choice [1,2,3,4,5]: ");
System.out.flush();
choiceString = in.readLine();
//convert string to integer
choice = Integer.parseInt(choiceString);
switch (choice)
{
//the choices go here - print the details
case 1:
System.out.println("\nDATE: " + c.get(Calendar.DATE) ); break;
case 2:
System.out.println("\nMONTH: " + monthString); break;
case 3:
System.out.println("\nYEAR: " + c.get(Calendar.YEAR)); break;
case 4:
System.out.println("\nDAY: " + dayString); break;
case 5: //exit
System.exit(0);
default:
System.out.println("\nPlease choose a number between 1-5 only
\n");
}
}
while(choice!= 5);
}
}
--------------------------------------------------------
Hope that helps! rickster
Last edited by cscgal : Aug 3rd, 2004 at 9:24 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- switch case (C)
- Switch Case Options (C#)
- Help with switch/case statement navigation (PHP)
- switch statement on String in Java (Java)
- switch/case statement (C++)
Other Threads in the Java Forum
- Previous Thread: Passing arguments to jsp file
- Next Thread: File I/O Manipulation Prob



Linear Mode