Kurt Bernhardt Kurt Bernhardt - 1 month ago 9
Java Question

I can't seem to declare this variable in java

I have to make a horoscope, and in the process I opted to declare a string then set it equal in an

if
statement.

I get an error on the last line, stating that
s
&
t
are not initialized. Am I missing something really simple? Any help is greatly appreciated.

import java.util.Scanner;

class main {
public static void main(String[] args)
{

Scanner scan = new Scanner(System.in);

System.out.println("What day of the month were you born? (number)");
int d = scan.nextInt();
System.out.println("Which month were you born? (number)");
int m = scan.nextInt();

if (m==3 && d>=21 && d<=31)
System.out.println("Your sign is Aries");
else if (m==4 && d<=19 && d>=1)
System.out.println("Your sign is Aries");
else if (m==4 && d>=20 && d<=31)
System.out.println("Your sign is Taurus");
else if (m==5 && d<=20 && d>=1)
System.out.println("Your sign is Taurus");
else if (m==5 && d>=21 && d<=31)
System.out.println("Your sign is Gemini");
else if (m==6 && d>=1 && d<=20)
System.out.println("Your sign is Gemini");
else if (m==6 && d>=21 && d<=31)
System.out.println("Your sign is Cancer");
else if (m==7 && d>=1 && d<=22)
System.out.println("Your sign is Cancer");
else if (m==7 && d>=23 && d<=31)
System.out.println("Your sign is Leo");
else if (m==8 && d>=1 && d<=22)
System.out.println("Your sign is Leo");
else if (m==8 && d>=23 && d<=31)
System.out.println("Your sign is Virgo");
else if (m==9 && d>=1 && d<=22)
System.out.println("Your sign is Virgo");
else if (m==9 && d>=23 && d<=31)
System.out.println("Your sign is Libra");
else if (m==10 && d>=1 && d<=22)
System.out.println("Your sign is Libra");
else if (m==10 && d>=23 && d<=31)
System.out.println("Your sign is Scorpio");
else if (m==11 && d>=1 && d<=21)
System.out.println("Your sign is Scorpio");
else if (m==11 && d>=22 && d<=31)
System.out.println("Your sign is Sagittarius");
else if (m==12 && d>=1 && d<=21)
System.out.println("Your sign is Sagittarius");
else if (m==12 && d>=22 && d<=31)
System.out.println("Your sign is Capricorn");
else if (m==1 && d>=1 && d<=19)
System.out.println("Your sign is Capricorn");
else if (m==1 && d>=20 && d<=31)
System.out.println("Your sign is Aquarius");
else if (m==2 && d>=1 && d<=18)
System.out.println("Your sign is Aquarius");
else if (m==2 && d>=19 && d<=31)
System.out.println("Your sign is Pisces");
else if (m==3 && d>=1 && d<=20)
System.out.println("Your sign is Pisces");
else
System.out.println("error");

String s;

if (m==1)
s=("January");
else if (m==2)
s=("February");
else if (m==3)
s=("March");
else if (m==4)
s=("April");
else if (m==5)
s=("May");
else if (m==6)
s=("June");
else if (m==7)
s=("July");
else if (m==8)
s=("August");
else if (m==9)
s=("September");
else if (m==10)
s=("October");
else if (m==11)
s=("November");
else if (m==12)
s=("December");

String t;

if (d==1)
t=("first");
else if (d==2)
t=("second");
else if (d==3)
t=("third");
else if (d==4)
t=("fourth");
else if (d==5)
t=("fifth");
else if (d==6)
t=("sixth");
else if (d==7)
t=("seventh");
else if (d==8)
t=("eighth");
else if (d==9)
t=("ninth");
else if (d==10)
t=("tenth");
else if (d==11)
t=("eleventh");
else if (d==12)
t=("twelfth");
else if (d==13)
t=("thirteenth");
else if (d==14)
t=("fourteenth");
else if (d==15)
t=("fifteenth");
else if (d==16)
t=("sixteenth");
else if (d==17)
t=("seventeenth");
else if (d==18)
t=("eighteenth");
else if (d==19)
t=("nineteenth");
else if (d==20)
t=("twentieth");
else if (d==21)
t=("twenty-first");
else if (d==22)
t=("twenty-second");
else if (d==23)
t=("twenty-third");
else if (d==24)
t=("twenty-fourth");
else if (d==25)
t=("twenty-fifth");
else if (d==26)
t=("twenty-sixth");
else if (d==27)
t=("twenty-seventh");
else if (d==28)
t=("twenty-eighth");
else if (d==29)
t=("twenty-ninth");
else if (d==30)
t=("thirtieth");
else if (d==31)
t=("thirty-first");

System.out.println("Your birthday is: " + s + " " + t);

}
}

Answer

Your issue comes from your long string of if-elseif code. Where you have

else if (d==31)
    t=("thirty-first");

Should be

else
    t = ("thirty-first");

and similarly for s. Java will always complain if your variables are only initialized within conditional statements, because it is possible that they will never be initialized. You could also initialize them with

String s = "";

to avoid this.

Comments