Pancake_Senpai
Pancake_Senpai

Reputation: 945

My java program won't run properly.

I'm a java beginner and I have written this code:

class Friends {
    public static void main(String[] args) {
        String[] facebookFriends = { "John", "Joe", "Jack", "Lucy", "Bob", "Bill", "Sam", "Will" };
        int x = 0;
        while (x <= 8) {
            System.out.println("Frind number " + (x + 1) + " is " + facebookFriends[x]);
            x++;
        }
        System.out.println("");

        if (facebookFriends.length < 5) {
            System.out.println("Where are all you're friends?");
        }
        else if (facebookFriends.length == 5) {
            System.out.println("You have a few friends...");
        }
        else {
            System.out.println("You are very sociable!");
        }
    }    
}

When I run the program it reads the names correctly but it doesn't display any of the text such as "You have a few friends..." or "You are very sociable!" Also, when I run it says "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8" between the third and forth name. I don't know what's wrong with my code but I would be grateful if anyone could tell me the problem. Thank you.

Upvotes: 1

Views: 388

Answers (5)

Eric
Eric

Reputation: 1297

As others have already pointed out, it should be x <= 7, x < 8 or better x < facebookFriends.length, as Java arrays are zero (0) based.

Another way of writing the code would be:

class Friends 
{
    public static void main(String[] args)
    {
        String[] facebookFriends = { "John", "Joe", "Jack", "Lucy", "Bob", "Bill", "Sam", "Will" };

        int length = facebookFriends.length;
        int num = 1;
        for ( String friend: facebookFriends )
            System.out.println("Friend number " + (num++) + " is " + friend);

        System.out.println("");

        if (length < 5)
            System.out.println("Where are all your friends?");
        else if (length == 5)
            System.out.println("You have a few friends...");
        else
            System.out.println("You are very sociable!");
    }    
}

Upvotes: 0

Vihar
Vihar

Reputation: 3831

if u really want to make it general purpose use while(x<=facebookFriends.length) this will ensure it works well even if number of friends in array are increased or decreased

Upvotes: 0

nanofarad
nanofarad

Reputation: 41271

while (x <= 8) {
   System.out.println("Frind number " + (x + 1) + " is " + facebookFriends[x]);
   x++;
}

tries to eventually read facebookFriends[8]. This is impossible as it goes from 0 to 7.

Use:

while (x < facebookFriends.length) {

instead.

Upvotes: 5

user2030471
user2030471

Reputation:

x <= 8 should be x < 8.

The facebookFriends array has 8 elements (having index from 0 to 7). Trying to access any position beyond this range will result in ArrayIndexOutOfBoundsException exception.

Upvotes: 1

rocketboy
rocketboy

Reputation: 9741

while (x <= 7) instead of while (x <= 8)

Arrays in Java, start from 0 and not 1.

If you look at the exception :

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8"

It tells you what went wrong.

Upvotes: 3

Related Questions