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));    
    }
}

Dani AI

Generated

Nice, simple exercise from with useful community notes (thanks for the input on normalizing month text). Main problems in the original approach: multiple Scanner instances, fragile string-replacement for month names, and off-by-one confusion from Calendar's 0-based month indexing. A clearer, more robust pattern is to normalize input, accept either a month name or a number, validate the date using the modern java.time API (Java 8+), and use a single Scanner.

Example (concise, safe pattern):

import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;
import java.util.Scanner;

Scanner in = new Scanner(System.in);
try {
    int year = Integer.parseInt(in.nextLine().trim());
    String m = in.nextLine().trim();
    int month;
    try {
        month = Integer.parseInt(m);
    } catch (NumberFormatException ex) {
        month = java.time.Month.valueOf(m.toUpperCase()).getValue();
    }
    int day = Integer.parseInt(in.nextLine().trim());
    LocalDate dob = LocalDate.of(year, month, day);
    System.out.println(dob.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
} catch (Exception ex) {
    System.out.println("Invalid input or date: " + ex.getMessage());
} finally {
    in.close();
}

Notes and troubleshooting tips: normalize inputs with trim()/toUpperCase() (as suggested); handle NumberFormatException and DateTimeException to catch bad months/days (Feb 30, etc.); avoid multiple Scanner(System.in) instances because closing one can close the underlying stream; if constrained to pre-Java‑8, use Calendar but remember human months are 1-based while Calendar months are 0-based (January == 0) and DAY_OF_WEEK values run 1 (SUNDAY) to 7 (SATURDAY).

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.