• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

whats wrong with this program

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}

}
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You avoid these index problems by using the Java 5 "for-each".
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, except that the question was "what's wrong with this program", not "make it work" ;)
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, I think the OP already said that.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic