DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Code Snippet: Birth DAY (of the week) finder (http://www.daniweb.com/forums/thread217133.html)

llemes4011 Aug 24th, 2008 8:58 pm
Birth DAY (of the week) finder
 
A program that finds the day of the week you were born on. Once again, this was adapted from a homework assignment, but I rewrote the entire thing from scratch >.< but it was a lot of fun.

  1. import java.util.GregorianCalendar;
  2. import java.lang.String;
  3. import java.util.Scanner;
  4.  
  5. /** Program that asks for your date of birth, then displays the weekday
  6.  * that you were born on (i.e., Sunday through Saturday)*/
  7.  
  8. public class birthDateFinder
  9. {
  10. public static void main(String[] args)
  11. { //Obtain the year of birth
  12.  
  13. System.out.println("What year were you born in?");
  14. Scanner keyboard = new Scanner(System.in);
  15. String varYear = keyboard.next();
  16.  
  17. int year = Integer.parseInt( varYear );
  18.  
  19. //Obtain the month of birth
  20.  
  21. System.out.println("What month were you born in? (Please type in ALL CAPS)");
  22. Scanner keyboard2 = new Scanner(System.in);
  23. String month = keyboard2.next();
  24.  
  25. //Obtain the day of the month of birth
  26.  
  27. System.out.println("What day of the month were you born on?");
  28. Scanner keyboard3 = new Scanner(System.in);
  29. String varDay = keyboard3.next();
  30.  
  31. int day = Integer.parseInt( varDay );
  32.  
  33. //========== Calculate day of birth ==========\\
  34.  
  35. //Change month into number (still concidered a String)
  36.  
  37. month = month.replace("JANUARY","0");
  38. month = month.replace("FEBRUARY","1");
  39. month = month.replace("MARCH","2");
  40. month = month.replace("APRIL","3");
  41. month = month.replace("MAY","4");
  42. month = month.replace("JUNE","5");
  43. month = month.replace("JULY","6");
  44. month = month.replace("AUGUST","7");
  45. month = month.replace("SEPTEMBER","8");
  46. month = month.replace("OCTOBER","9");
  47. month = month.replace("NOVEMBER","10");
  48. month = month.replace("DECEMBER","11");
  49.  
  50. //Change month into int
  51.  
  52. int realMonth = Integer.parseInt( month );
  53.  
  54. //create the calendar object
  55.  
  56. GregorianCalendar birthDay = new GregorianCalendar();
  57.  
  58. //set the calendar with the previously assigned variables
  59.  
  60. birthDay.set(year, realMonth, day);
  61.  
  62. //Get the weekday as a number 0-6
  63.  
  64. int weekday = birthDay.get(GregorianCalendar.DAY_OF_WEEK);
  65.  
  66. //convert GregorianCalendar.DAY_OF_WEEK into words
  67.  
  68. String weekDay = String.valueOf(weekday -1);
  69.  
  70. weekDay = weekDay.replace("0","Sunday.");
  71. weekDay = weekDay.replace("1","Monday.");
  72. weekDay = weekDay.replace("2","Tuesday.");
  73. weekDay = weekDay.replace("3","Wednesday.");
  74. weekDay = weekDay.replace("4","Thursday.");
  75. weekDay = weekDay.replace("5","Friday.");
  76. weekDay = weekDay.replace("6","Saturday.");
  77.  
  78. //print the weekday of birth
  79.  
  80. System.out.println("You were born on a " +(weekDay));
  81. }
  82. }
uringinteristi Dec 28th, 2008 9:05 am
thank you

slnvpraveen Feb 20th, 2009 1:28 pm
good effort!!!
but if we enter the month in number format it's not throwing any error and leading to wrong result.except that every thing is fine...
as u specified enter the month in all caps...I think ...u can ignore that by using the function toUpperCase() ....
overall nice programming!!!!!

llemes4011 Feb 22nd, 2009 9:03 pm
thanks :) this was one of the first things that i wrote so... yeah, I knew next to nothing about the String class. I knew about toString() but that was about it, lol.

If you count January as 0, the month is found correctly, so, yeah, lol, it needs work XD

toyi Jun 15th, 2009 8:26 am
Jah good work , keep on diong so .


All times are GMT -4. The time now is 6:44 am.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC