Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whats wrong with this program

 
vidu sri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is an exercise question
i dont understand the explanation that has been given
class Hello {

public static void main (String args[]) {

int i;

System.out.print("Hello ");
i = 0;
while (i <= args.length) {
System.out.print(args[i] + " ");
i = i + 1;
}
System.out.println();
}

}
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vidu sri wrote:this is an exercise question
i dont understand the explanation that has been given


Can you show us the explanation? Where you don't understand?

 
vidu sri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program encounters an ArrayIndexOutOfBoundsException when i becomes eqaul to args.length. Array indexes start at zero and count to one less than the length of the array as in C and not at 1 as in Fortran. The <= sign should just be a <.

What happens if you don't give Program 3.10 any command line arguments? You aren't testing the number of command line arguments anymore so why isn't an ArrayIndexOutOfBoundsException thrown?
i is initialized to zero. If the length of the array is zero, then i is not less than args.length, so it never enters the loop and it never tries to read an array element that isn't there.


For math whizzes only: I lied. In certain interpretations of certain number systems the statement i = i + 1 does have a valid solution for i. What is it?
If infinity is considered to be a number, then infinity + 1 equals infinity.

Joshua Davis of Oberllin College's Mathematics Department suggested the alternate and equally valid answer "When '+' represents a group operator, and the group is, for example, the reals under multiplication.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vidu sri wrote:What happens if you don't give Program 3.10 any command line arguments? You aren't testing the number of command line arguments anymore so why isn't an ArrayIndexOutOfBoundsException thrown?
i is initialized to zero. If the length of the array is zero, then i is not less than args.length, so it never enters the loop and it never tries to read an array element that isn't there.

Sure the loop is entered. If args.length == 0 and i == 0 (initially), i <= args.length.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You avoid these index problems by using the Java 5 "for-each".
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, except that the question was "what's wrong with this program", not "make it work" ;)
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is <= should be <

args[args.length] is always going to cause an out-of-bounds exception no matter how many args there actually are. In Java (as in C and C++) arrays are indexed from 0 to length - 1.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I think the OP already said that.
 
Edgar Balderas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:The problem is <= should be <

args[args.length] is always going to cause an out-of-bounds exception no matter how many args there actually are. In Java (as in C and C++) arrays are indexed from 0 to length - 1.


He is right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic