Kaden
Kaden

Reputation: 13

String inside switch case returns null

All variables are correctly initialized, and are only defined inside this one method, calls day as an integer, then sets to a string to be able to be read and then split into characters to create the word format. Also, mathematics with dayL, an integer, sometimes returns the StringIndexOutOfBounds Exception, which i understand to be due to an error due to the inequality i create with the .length(); Thank you for any help.

    public static void bday()
{
    s_day = Integer.toString(day);
    dayL = s_day.length();

    switch (dayL)
    {
    case 1:
        if(s_day.charAt(0) == 1)
        {
            word_day = "first";
        }
        else if(s_day.charAt(0) == 2)
        {
            word_day = "second";
        }
        else if(s_day.charAt(0) == 3)
        {
            word_day = "third";
        }
        else if(s_day.charAt(0) == 4)
        {
            word_day = "fourth";
        }
        else if(s_day.charAt(0) == 5)
        {
            word_day = "fifth";
        }
        else if(s_day.charAt(0) == 6)
        {
            word_day = "sixth";
        }
        else if(s_day.charAt(0) == 7)
        {
            word_day = "seventh";
        }
        else if(s_day.charAt(0) == 8)
        {
            word_day = "eighth";
        }
        else if(s_day.charAt(0) == 9)
        {
            word_day = "ninth";
        }
        break;

    case 2:
        //teens

        if(s_day.charAt(0) == 1 && s_day.charAt(1) == 0)
        {
            word_day = "tenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 1)
        {
            word_day = "eleventh";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 2)
        {
            word_day = "twelfth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 3)
        {
            word_day = "thirteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 4)
        {
            word_day = "fourteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 5)
        {
            word_day = "fifteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 6)
        {
            word_day = "sixteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 7)
        {
            word_day = "seventeenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 8)
        {
            word_day = "eighteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 9)
        {
            word_day = "ninteenth";
        }

        //twenties

        if(s_day.charAt(0) == 2 && s_day.charAt(1) == 0)
        {
            word_day = "twentieth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 1)
        {
            word_day = "twenty-first";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 2)
        {
            word_day = "twenty-second";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 3)
        {
            word_day = "twenty-third";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 4)
        {
            word_day = "twenty-fourth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 5)
        {
            word_day = "twenty-fifth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 6)
        {
            word_day = "twenty-sixth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 7)
        {
            word_day = "twenty-seventh";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 8)
        {
            word_day = "twenty-eighth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 9)
        {
            word_day = "twenty-ninth";
        }

        //thirties

        if(s_day.charAt(0) == 3 && s_day.charAt(1) == 0)
        {
            word_day = "thirtieth";
        }
        else if(s_day.charAt(0) == 3 && s_day.charAt(1) == 1)
        {
            word_day = "thirty-first";
        }
        break;
    }
    System.out.println("Your birthday is: " + s_month + " "+ word_day);
}

Upvotes: 0

Views: 57

Answers (3)

Spotted
Spotted

Reputation: 4091

Please refactor this code like @fei_hsueh proposed or like below:

Map<Integer, String> days = new HashMap<>();
days.put(1, "first");
days.put(2, "second");
days.put(3, "third");
...

private static String bday(int day) {
    return days.get(day);
}

Upvotes: 0

fei_hsueh
fei_hsueh

Reputation: 171

You should use

s_day.charAt(0)=='1'

And I recommend that you can write thie method like this:

String [] days={"first","second","third",.......,"thirty-first"};
word_day=days[day%31];

Upvotes: 1

nhouser9
nhouser9

Reputation: 6780

Every time you compare a character, you do it wrong.

s_day.charAt(0) == 1

should be

s_day.charAt(0) == '1'

But even that is way more complicated than it needs to be. You have the day as an int in the variable day, right? So why not just make an if statement based on that:

if (day == 1) {
  word_day = "first";
}else if (day == 2)
//and so on

Upvotes: 3

Related Questions