user3353723
user3353723

Reputation: 219

Trying to use Switch statements within a loop in Java

I am trying to get practice on loops and switch statements and trying to figure out the following issue.

The output depends on the integer day value. For example if the user enters 8 then it will print the first 8 lines as follows:

On the 8th day of holidays, I did Eight jumping jacks,
....
...
On the 3rd day of holidays, I did Three pull downs,
...
..
On the 1st day of holidays, I did One downhill sprint.

So to solve this problem, I have used a switch statement in a for loop and then wanted to use another switch statement to put the appropriate suffix for the day number. Eg: 1st, 2nd etc

I have done the following but I am stuck badly and would really appreciate if they can help me out.

 int day = in.nextInt();
        for (int i = 0; i < day; i++) {
            switch (day) {

                // Also, do I add another switch statement here for suffix?


            }
        }

Upvotes: 1

Views: 3260

Answers (5)

ifloop
ifloop

Reputation: 8386

To get the right suffix you could use a switch:

private static String getSuffix(final int number) {
    switch (number) {
        case 0: throw new IllegalArgumentException();
        case 1: return "st";
        case 2: return "nd";
        case 3: return "rd";
        default:return "th";
    }
}

To geht the word form of a number you could use an array:

final static String[] numbers = new String[] {
        "zero", "one", "two", "three", "four", 
        "five", "six", "seven", "eight", "nine", "ten"
};

To use this, just append numbers[yourInt] to your string.
numbers[5] e.g. will be "five".



Putting everything together might look like this (It also appends an s to the activity if needed):

public class Main {
    final static String[] numbers = new String[] {
            "zero", "one", "two", "three", "four", 
            "five", "six", "seven", "eight", "nine", "ten"
    };

    final static String  pattern = "On the %d%s day of holidays, I did %s %s%s\n";
    final static Scanner in      = new Scanner(System.in);

    public static void main(String[] args) {
        final int    day = in.nextInt();
        final String activity;

        switch (day) {
            case 1  : activity = "downhill sprint";  break;
            // ...
            case 3  : activity = "pull down";        break;
            //...
            case 8  : activity = "jumping jack";     break;
            default : activity = "";
        }

        if (!activity.equals(""))
            System.out.printf(pattern, day, getSuffix(day), numbers[day], activity, day > 1 ? "s" : "");

    }

    private static String getSuffix(final int number) {
        switch (number) {
            case 0: throw new IllegalArgumentException();
            case 1: return "st";
            case 2: return "nd";
            case 3: return "rd";
            default:return "th";
        }
    }
}

Upvotes: 1

Joel
Joel

Reputation: 1630

Converting the number to a word can be done with a switch statement, in the case where you are having a limited (and small) number. For example, only numbers one to 10.

It would be as follows:

String numberWord;

switch (day)
{
case 1:
    suffix = "one";
    break;
case 2:
    suffix = "two";
    break;
case 3:
    suffix = "three";
    break;

    //ETC

default: break;
}

However, if you wish for a much larger range of numbers, I recommend you check out the following page: http://www.rgagnon.com/javadetails/java-0426.html

It contains a method which converts a number to a word. It converts into the billions.

Regarding the suffix, a similar solution to that of above can be used. Watch out for 11, 12 and 13 though, because they have their unique suffixes.

String GetSuffix (int number)
{
    int lastTwoDigits = number % 100;

    if (lastTwoDigits == 11 || lastTwoDigits == 12 || lastTwoDigits == 13)
    {
        return "th";
    }

    switch (number % 10)
    {
    case 1:
        suffix = "st";
        break;
    case 2:
        suffix = "nd";
        break;
    case 3:
        suffix = "rd";
        break;
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 0:
        suffix = "th";
        break;

    default: break;
    }

    //This shouldn't happen
    return "";
}

Here's an example of how to use this:

String result = "On the " + Integer.toString(day) + GetSuffix(day) + " day of holidays, I did " + EnglishNumberToWords(day) + " jumping jacks";

Upvotes: 0

sakura
sakura

Reputation: 2279

Loop is not required, you can do it as follows:

int day = in.nextInt();

switch(day){
    case 10:
        System.out.println("statement 10");       
    case 9:
        System.out.println("statement 9");
    case 8:
        System.out.println("statement 8");
    case 7:
        System.out.println("statement 7");
    case 6:
        System.out.println("statement 6");
    case 5:
        System.out.println("statement 5");
    case 4:
        System.out.println("statement 4");
    case 3:
        System.out.println("statement 3");
    case 2:
        System.out.println("statement 2");
    case 1:
        System.out.println("statement 1");
    }

So, when the input is 8, it will print your all statements starting from 8 to 1 as you require.

Upvotes: 2

Akshay Abhyankar
Akshay Abhyankar

Reputation: 599

Assume day has value entered by user.. then..

for(int i=day;i<=day && i!=0;i--)
{
switch(i)
{
case 8 :
On the 8th day of holidays, I did Eight jumping jacks,
break;
.
.
.

case 3 :
On the 3rd day of holidays, I did Three pull downs,
break;
.
.

case 1 :
On the 1st day of holidays, I did One downhill sprint.
break;

default :
break;
}
}

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53829

Use methods for each operation. Each method will have its own switch:

private String getDaySuffix(int day) {
    switch(day) {
    case 1: return "st";
    // ...
    }
}

private String getActivity(int day) {
    switch(day) {
    case 1: return "One downhill sprint";
    // ...
    }
}

for (int i = 0; i < day; i++) {
    String s = "On the " + day + getDaySuffix(day) + 
               " day of holidays, I did " + getActivity(day);
}

That way, you improve cohesion: each method does what it is supposed to do, and nothing else.

Upvotes: 1

Related Questions