• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array index out of bound problem

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this program


class Rectangle{
int length, breadth;
void show(int x, int y){
length = x;
breadth = y;
}
int calculate(){
return(length * breadth);
}
}
public class Main {


public static void main(String[] args) {

System.out.println("enter the value");
Rectangle rectangle = new Rectangle();
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
rectangle.show(a, b);
System.out.println(" you have entered these values : " + a + " and " + b);
int area = rectangle.calculate();
System.out.println(" area of a rectange is : " + area);
}

}

while executing this i am gettin array out of bound exception,can some body tell me what can i do for this.

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please use code tags..


Are you passing command line arguments while compiling this code ?
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry,i will use from now onwards.

No command line arguments is passed
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone please reply to this immediately.
 
Prafull Prashant
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have not specified any arguments and you are trying to retrieve values from argument array, its throwing exception.


Always check for argument length before processing. For Example:
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks prafull,


so where should i alter the program.could to rewrite only that part and post it.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you compiling this program from command line or from eclipse/netbeans?
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from netbeans i am compiling.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:


CAUTION: there is a misspelling in the code. args.lentgh should be args.length.

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi neha,i have just tried compiling in netbeans,its compiling but its not asking for an input,what to do for that?
 
Prafull Prashant
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Neha,
How does the ide matter in this case?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijay umar wrote:can someone please reply to this immediately.

Vijay please Ease Up. In Netbeans if you go into the properties of the project and go into the "Run" section, there is an option to pass command line arguments to the application, google for more info...
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prafull Prashant wrote:Hey Neha,
How does the ide matter in this case?


I think Neha asked if Vijay was using command line vs. IDE. Since Vijay is using NetBeans, he should select the Project properties' run to set the arguments.
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean like we have to run the program,ctrl+f6.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How hard is it to search for command line arguments netbeans on google?? The first result is this...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic