I've been outta school for two weeks and I've been working on this program from my Com Sci class for about three hours. It's supposed to be something that will tell you what day you were born on. For some reason I keep getting the error:
I can't seem to find what's wrong. Here's my code:C:\Java\Birthday.java:53: error: variable total2 might not have been initialized
Total2 = String.valueOf(total2);
^
1 error
Process completed.
import java.lang.String; import java.util.Scanner; public class Birthday { public static void main(String[]arg) { Scanner sc = new Scanner(System.in); int birthy; int birthyd; int birthd; double total2; String birthm; String leap; String name1 = "YES"; String name2 = "NO"; System.out.println("What year were you born in (two digits)?"); birthy = sc.nextInt(); System.out.println("What month were you born in (all caps)?"); birthm = sc.next(); birthm = birthm.replace("JANUARY","1"); birthm = birthm.replace("FEBRUARY","4"); birthm = birthm.replace("MARCH","4"); birthm = birthm.replace("APRIL","0"); birthm = birthm.replace("MAY","2"); birthm = birthm.replace("JUNE","5"); birthm = birthm.replace("JULY","0"); birthm = birthm.replace("AUGUST","3"); birthm = birthm.replace("SEPTEMBER","6"); birthm = birthm.replace("OCTOBER","4"); birthm = birthm.replace("NOVEMBER","4"); birthm = birthm.replace("DECEMBER","6"); int real = Integer.parseInt( birthm ); System.out.println("What day of the month were you born on?"); birthd = sc.nextInt(); System.out.println("Were you born on a leap year (YES or NO?)"); leap = sc.next(); if (leap.equals(name1)){ total2 = (((birthy/4) + birthy + real - 1)%7); } else{ if (leap.equals(name2)){ total2 = (((birthy/4) + birthy + real - 0)%7); } } String Total2; Total2 = String.valueOf(total2); Total2 = Total2.replace("1","Sunday."); Total2 = Total2.replace("2","Monday."); Total2 = Total2.replace("3","Tuesday."); Total2 = Total2.replace("4","Wednesday."); Total2 = Total2.replace("5","Thursday."); Total2 = Total2.replace("6","Friday."); Total2 = Total2.replace("0","Saturday."); System.out.println("You were born on a" +(Total2)); } }
And when I try anything out, I keep getting the day Wednesday. I'm not sure what I'm doing wrong.
This is what my teacher attached to my email, it's the instructions he wanted me to follow.
Anything will help, thanks for your time.3) Create a new file named Birthday.java.
Write a program to determine the day of the week that a person was born given his or her birth date. Following are the steps you should use to find the day of the week corresponding to any date in the 20th century.
a. Divide the last two digits of the birth year by 4. Put the quotient (ignoring the remainder) in total. (For example, if the person was born in 1983, divide 83 by 4 and store 20 in total.)
b. Add the last two digits of the birth year to total.
c. Add the birth day of the month to total.
d. Use the following table, find the “month number” and add it to total.
Jan = 1 Feb = 4 Mar = 4 Apr = 0 May = 2 Jun = 5
July = 0 Aug = 3 Sept = 6 Oct = 1 Nov = 4 Dec = 6
e. If the year is a leap year and if the month you are working with is either Jan or Feb, then subtract 1 from total.
f. Find the remainder when total is divided by 7. Look up the remainder in the following table to determine the day of the week the person was born. Note that you cannot use this procedure if a person was born before 1900;
1 = Sunday 2 = Monday 3 = Tuesday 4 = Wednesday
5 = Thursday 6 = Friday 0 = Saturday
The user should be prompted to enter the birth month by number( 1 for Jan, 2 for Feb...) then the birth date and last the year in the format 1983. Make sure to give an error message if the birth year is before 1900.