943,786 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 99898
  • Java RSS
Jul 25th, 2004
0

Switch Case Statement

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shantuli is offline Offline
16 posts
since Jul 2004
Jul 26th, 2004
0

Re: Switch Case Statement

Read about the Time class ... you'll get what you want.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Jul 27th, 2004
1

Re: Switch Case Statement

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.

-------------------------------------------------------------------------------

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.io.IOException;
  3. import java.util.*;
  4.  
  5.  
  6. public class Test
  7. {
  8. public static void main(String[] args) throws IOException
  9. {
  10. //create a bufferedReader object
  11. BufferedReader in = new BufferedReader(new
  12. InputStreamReader(System.in));
  13.  
  14. String choiceString;
  15. int choice;
  16.  
  17. //create a Calender object
  18. Calendar c = Calendar.getInstance();
  19.  
  20. //create an array to hold day names
  21. //these cant be changed later therefore are declared final.
  22. final String dayNames[] =
  23. {"Monday",
  24. "Tuesday",
  25. "Wednesday",
  26. "Thursday",
  27. "Friday",
  28. "Saturday",
  29. "Sunday"};
  30.  
  31. //create an array to hold month names
  32. //these cant be changed later therefore are declared final.
  33. final String monthNames[] =
  34. {"January",
  35. "February",
  36. "march",
  37. "April",
  38. "May",
  39. "June",
  40. "July",
  41. "August",
  42. "September",
  43. "October",
  44. "Novemeber",
  45. "December"};
  46.  
  47. //java notation for MONTH is 0-11, where 0 = January
  48. int month = (c.get(Calendar.MONTH) + 1);
  49.  
  50. //java notation for DAY_OF_WEEK is 0-6, where 0 = Sunday
  51. int day = (c.get(Calendar.DAY_OF_WEEK) -1);
  52.  
  53. //variable to hold day names
  54. String dayString = dayNames[day-1];
  55.  
  56. //variable to hold month names
  57. String monthString = monthNames[month-1];
  58.  
  59. //consistantly ask for user input
  60. do
  61. {
  62. //print out a menu
  63. System.out.println("\n------- Menu----------");
  64. System.out.println("1. DATE");
  65. System.out.println("2. MONTH");
  66. System.out.println("3. YEAR");
  67. System.out.println("4. DAY");
  68. System.out.println("5. Quit");
  69. System.out.println("Enter choice [1,2,3,4,5]: ");
  70.  
  71. System.out.flush();
  72. choiceString = in.readLine();
  73.  
  74. //convert string to integer
  75. choice = Integer.parseInt(choiceString);
  76.  
  77. switch (choice)
  78. {
  79. //the choices go here - print the details
  80. case 1:
  81. System.out.println("\nDATE: " + c.get(Calendar.DATE) ); break;
  82. case 2:
  83. System.out.println("\nMONTH: " + monthString); break;
  84. case 3:
  85. System.out.println("\nYEAR: " + c.get(Calendar.YEAR)); break;
  86. case 4:
  87. System.out.println("\nDAY: " + dayString); break;
  88. case 5: //exit
  89. System.exit(0);
  90.  
  91. default:
  92. System.out.println("\nPlease choose a number between 1-5 only
  93. \n");
  94. }
  95. }
  96. while(choice!= 5);
  97.  
  98. }
  99. }

--------------------------------------------------------
Hope that helps! rickster
Last edited by Nick Evan; Mar 12th, 2010 at 3:43 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rickste_r is offline Offline
16 posts
since Jul 2004
Mar 12th, 2010
0
Re: Switch Case Statement
Click to Expand / Collapse  Quote originally posted by shantuli ...
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

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. class Switch
  3. {
  4. public static void main(String []pp)throws IOException
  5. {
  6. try
  7. {
  8. BufferedReader object = new BufferedReader
  9. (new InputStreamReader(System.in));
  10. System.out.println("1. Date");
  11. System.out.println("2. Month");
  12. System.out.println("3. Year");
  13. System.out.println("4. Day");
  14. System.out.println("enter your choice:");
  15. int a= Integer.parseInt(object.readLine());
  16. switch (a){
  17. case 1:
  18. System.out.println("Date");
  19. break;
  20. case 2:
  21. System.out.println("Month");
  22. break;
  23. case 3:
  24. System.out.println("Year");
  25. break;
  26. case 4:
  27. System.out.println("Day");
  28. break;
  29. default:
  30. System.out.println("Invalid Entry!");
  31. }
  32. }
  33. catch(NumberFormatException ne){
  34. System.out.println(ne.getMessage() + " is not a numeric value.");
  35. System.exit(0);
  36. }
  37.  
  38. }
  39. }
From
Jaspreet Singh
Last edited by Nick Evan; Mar 12th, 2010 at 3:42 pm. Reason: Added code-tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jaspreet Singh is offline Offline
1 posts
since Mar 2010
Mar 12th, 2010
0
Re: Switch Case Statement
[..code..]
From
Jaspreet Singh

Good job! Only six years late and no code-tags
I wish more people read the rules before posting
closed
Last edited by Nick Evan; Mar 12th, 2010 at 3:44 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Switch Statement Help
Next Thread in Java Forum Timeline: New to Java and need help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC