Birth DAY (of the week) finder

llemes4011 0 Tallied Votes 2K Views Share

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.

import java.util.GregorianCalendar;
import java.lang.String;
import java.util.Scanner;

/** Program that asks for your date of birth, then displays the weekday
 *  that you were born on (i.e., Sunday through Saturday)*/

public class birthDateFinder
{
    public static void main(String[] args)
    {   //Obtain the year of birth
        
        System.out.println("What year were you born in?");
        Scanner keyboard = new Scanner(System.in);
        String varYear = keyboard.next();
        
        int year = Integer.parseInt( varYear );
                
        //Obtain the month of birth
        
        System.out.println("What month were you born in? (Please type in ALL CAPS)");
        Scanner keyboard2 = new Scanner(System.in);
        String month = keyboard2.next();
        
        //Obtain the day of the month of birth
        
        System.out.println("What day of the month were you born on?");
        Scanner keyboard3 = new Scanner(System.in);
        String varDay = keyboard3.next();
        
        int day = Integer.parseInt( varDay );
        
        //========== Calculate day of birth ==========\\
        
        //Change month into number (still concidered a String)
        
        month = month.replace("JANUARY","0");
        month = month.replace("FEBRUARY","1");
        month = month.replace("MARCH","2");
        month = month.replace("APRIL","3");
        month = month.replace("MAY","4");
        month = month.replace("JUNE","5");
        month = month.replace("JULY","6");
        month = month.replace("AUGUST","7");
        month = month.replace("SEPTEMBER","8");
        month = month.replace("OCTOBER","9");
        month = month.replace("NOVEMBER","10");
        month = month.replace("DECEMBER","11");
        
        //Change month into int
        
        int realMonth = Integer.parseInt( month );
        
        //create the calendar object
        
        GregorianCalendar birthDay = new GregorianCalendar();
        
        //set the calendar with the previously assigned variables
        
        birthDay.set(year, realMonth, day);
        
        //Get the weekday as a number 0-6
        
        int weekday = birthDay.get(GregorianCalendar.DAY_OF_WEEK);
        
        //convert GregorianCalendar.DAY_OF_WEEK into words
        
        String weekDay = String.valueOf(weekday -1);
        
        weekDay = weekDay.replace("0","Sunday.");
        weekDay = weekDay.replace("1","Monday.");
        weekDay = weekDay.replace("2","Tuesday.");
        weekDay = weekDay.replace("3","Wednesday.");
        weekDay = weekDay.replace("4","Thursday.");
        weekDay = weekDay.replace("5","Friday.");
        weekDay = weekDay.replace("6","Saturday.");        

        //print the weekday of birth
        
        System.out.println("You were born on a " +(weekDay));    
    }
}
uringinteristi 0 Newbie Poster

thank you

slnvpraveen 0 Newbie Poster

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 31 Posting Whiz in Training

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 0 Newbie Poster

Jah good work , keep on diong so .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.