1

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?


            }
        }
user3353723
  • 221
  • 1
  • 7
  • 15
  • I am a little confused as I don't see a System.out.println() statement anywhere here. You have, as I see it, 2 things changing ... you have the xth day, which you could create by joining a string to a temporary variable, and you have the word "Eight", "Seven", etc., which you could store and look-up. Why are you using the switch statement & could you do without it (or is it a firm requirement for inclusion)? – Nathaniel Payne Apr 08 '14 at 07:23
  • This question has already been answered by [this answer](http://stackoverflow.com/a/6810409/256196). – Bohemian Apr 08 '14 at 09:59

5 Answers5

2

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.

sakura
  • 2,249
  • 2
  • 26
  • 39
  • Thanks for the reply but I know how to print standard S.o.p statements but how will I check for the suffix using another switch – user3353723 Apr 08 '14 at 07:29
  • 1
    @user3353723 With this approach you wouldn't need to check the suffix. The string given to each println would be the complete string for each case. E.g. `case 8: System.out.println("On the 8th day of holidays, I did Eight jumping jacks,");` Note that this relies on the fall-through of case statments, which might not be considered good style. – Emil L Apr 08 '14 at 07:35
  • @user3353723 yes its fall-through and it will print all the cases starting from the matching one. In general programming scenario, we don't have such requirements and you can consider it as a sample only to understand the language. – sakura Apr 08 '14 at 07:46
1

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.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

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;
}
}
Akshay Abhyankar
  • 599
  • 1
  • 4
  • 11
1

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";
        }
    }
}
ifloop
  • 8,079
  • 2
  • 26
  • 35
0

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";
Joel
  • 1,580
  • 4
  • 20
  • 33