0

I have programmed following class in Java

public class Factorial
    {
        final String[] promotion: {"watch", "on", "youtube:", "Mickey en de stomende drol", (https://www.youtube.com/watch?v=a3leCIk2eyQ)"};
    public static void main(String[] args)
    {   
        System.out.println(shoutPromotion(promotion));
    }

    public static String shoutPromotion(String[] promotion)
    {   String result = "";
        for(int i = 1; i < promotion.length; i++)
            result += promotion[i] + " ";
        return result;
    }
}

But when I run the program I see following output in the console:

on youtube: Mickey en de stomende drol (https://www.youtube.com/watch?v=a3leCIk2eyQ)

The word watch disappeeard. How does this come?

2
  • 1
    i = 1 <-- look at this more closely; Commented Jan 14, 2016 at 11:21
  • 1
    arrays start with the index 0 and not 1, and since you start your loop at i=1 you skip the first element Commented Jan 14, 2016 at 11:21

1 Answer 1

3

Array index starts with 0 not 1.

This

for(int i = 1; i < promotion.length; i++)

Change it to

for(int i = 0; i < promotion.length; i++)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.