0
public class Hobbits {
 String name;

 public static void main(String[] args){

  Hobbits [] h = new Hobbits[3];
  int z = 0;

  while (z<4) {
   z = z + 1;
   h[z] = new Hobbits();
   h[z].name = "Bilbo";
   if (z == 1) {
    h[z].name = "Frodo";
   }
   if (z == 2){
    h[z].name = "Sam";
   }
   System.out.print(h[z].name+ " is a ");
   System.out.println("good Hobbit name");

  }
 }
}

At runtime this program is giving the exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
5
  • 3
    Some exception, you say? Well, then something is wrong. :) (hint: be more specific) Commented Jan 15, 2011 at 15:53
  • Hi. Can you paste the exception here? Commented Jan 15, 2011 at 15:54
  • 1
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 Frodo is a good Hobbit name Sam is a good Hobbit name at Hobbits.main(Hobbits.java:12) Commented Jan 15, 2011 at 15:54
  • 1
    but what has it got in its pocketses, precious? Commented Jan 15, 2011 at 15:57
  • You can use a for loop and it will do the index incrementing in a cleaner manner. Also, use z < h.length as the loop condition and you will never overrun an array. Commented Jan 15, 2011 at 16:00

9 Answers 9

3

Agree with the comment, you should've been more specific, however by looking at the code I can see that your loop runs from 1 to 3 and your array has a length of only 3 (so the highest available index is 2).

Sign up to request clarification or add additional context in comments.

Comments

2

H has h[0], h[1] and h[2]. You're asking for h[3] there :)

Comments

2

Your array is 3 long.

1: Array indices start at 0.

2: Your code goes up to index 4 when 2 is the highest in your array:

 while (z<4) {
       z = z + 1;
       h[z] = new Hobbits();
       ...
 }

Comments

1

I guess you are getting IndexOutOfBoundsException - and it, because you are incrementing z before creating h[z] instance. just increment in the end of the loop.

2 Comments

If I increment z in the end of the loop, still I am getting exception belowBilbo is a good Hobbit name Frodo is a good Hobbit name Sam is a good Hobbit name Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Hobbits.main(Hobbits.java:11)
Noray is right, your array has a length of 3 meaning it cna have index 0, 1 and 2. So the condition must be that z doesn't reach 3
1

you need to move z = z + 1; to the end of the while loop

2 Comments

This will not solve the problem because the code will still go to index 3 with is still out of bounds.
Correct, sysout at the end will throw an exception. that should also be converted to [z-1]
1

For your interest, a shorter version might look like this.

String[] names = "Bilbo,Frodo,Sam".split(",");
Hobbits[] h = new Hobbits[names.length];
for(int i=0;i<names.length;i++) {
   h[i] = new Hobbits();
   h[i].name = names[i];
   System.out.print(h[i].name+ " is a good Hobbit name");
}

The class Hobbits is redundant and you would get the same output with.

for(String name: "Bilbo,Frodo,Sam".split(","))
   System.out.print(name+ " is a good Hobbit name");

Comments

1
  1. move z=z+1 to the end of the loop
  2. change the condition of the while loop to z<3

Comments

0

The exception is because of subscript error, but what is the point of having such a class?
I think that it would be a better approach to have a Hobbit class with one or two constructors some accessors and some mutators. Then it would make sense to add either a main or a TryHobbit class with a main that does its tricks with the array etc. That would provide a "cleaner" learning example.

Comments

0

Try this and it should work.

int  z  =  -1
while   (z  < 2)   

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.