Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I'm either doing this entirely wrong or I'm missing something simple.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default I'm either doing this entirely wrong or I'm missing something simple.

    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:

    C:\Java\Birthday.java:53: error: variable total2 might not have been initialized
    Total2 = String.valueOf(total2);
    ^
    1 error

    Process completed.
    I can't seem to find what's wrong. Here's my code:
    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.
    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.
    Anything will help, thanks for your time.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: I'm either doing this entirely wrong or I'm missing something simple.

    lol
    your problem is because u didnt initialalized field total2. u must set a number in line where u have double total2;
    C:\Java\Birthday.java:53: error: variable total2 might not have been initialized
    Total2 = String.valueOf(total2);
    this error say everything about your problem

Similar Threads

  1. What's wrong with simple Scanner program?
    By SV25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 21st, 2011, 07:05 AM
  2. Missing drivers?
    By mjpam in forum JDBC & Databases
    Replies: 5
    Last Post: September 6th, 2010, 08:00 PM
  3. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM
  4. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 07:49 PM
  5. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 12:33 PM