orange
orange

Reputation: 5405

Method issue using switch statement, return not working

Here is the code:

    import java.util.*;

public class dayName {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the number to find out what day of the week it represents:");
        int number = in.nextInt();
        weekNumber(number);
    }

    public static String weekNumber(int number)
    {
        String dayNumber;
        switch (number)
        {
        case 1: dayNumber = "Monday"; break;
        case 2: dayNumber = "Tuesday"; break;
        case 3: dayNumber = "Wednesday"; break;
        case 4: dayNumber = "Thursday"; break;
        case 5: dayNumber = "Friday"; break;
        case 6: dayNumber = "Saturday"; break;
        case 7: dayNumber = "Sunday"; break;
        default: dayNumber = ""; break;
        }

        return dayNumber;
    }

}

My problem is that it compiles fine but when you ask it the number it doesn't return anything, it should return the day string depending on the number you enter.

Please bear in mind I'm learning Java.

Upvotes: 1

Views: 8663

Answers (3)

BenCole
BenCole

Reputation: 2112

You have to write the name to the console:

System.out.println(weekNumber(number));

Upvotes: 2

Mechkov
Mechkov

Reputation: 4324

Where are you asking for the number? if you are asking it in the main method this line of code:

weekNumber(number);

does not assign your String into a String variable.

Try this in the main method

System.out.println(weekNumber(number));

Hope this helps!

Upvotes: 1

Reverend Gonzo
Reverend Gonzo

Reputation: 40811

weekNumber() just returns the value but you don't do anything with it.

Change that line to:

System.out.println(weekNumber(number));

Upvotes: 6

Related Questions