jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes whats wrong with this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "whats wrong with this program" Watch "whats wrong with this program" New topic
Author

whats wrong with this program

vidu sri
Greenhorn

Joined: Jun 15, 2009
Posts: 8
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

Joined: Mar 24, 2008
Posts: 3775

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?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
vidu sri
Greenhorn

Joined: Jun 15, 2009
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19557
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
You avoid these index problems by using the Java 5 "for-each".
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sure, except that the question was "what's wrong with this program", not "make it work" ;)
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2774
    
  10

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

Joined: Sep 29, 2008
Posts: 12617

Hmm, I think the OP already said that.
Edgar Balderas
Greenhorn

Joined: Jun 13, 2009
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats wrong with this program
 
Similar Threads
String.intern()-regd
Overriding
static initiliazer
substring confusion...
A little help on looping