This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array index out of bound problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array index out of bound problem" Watch "array index out of bound problem" New topic
Author

array index out of bound problem

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
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

Joined: Oct 30, 2009
Posts: 504
please use code tags..


Are you passing command line arguments while compiling this code ?


SCJP 1.6 96%
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
oh sorry,i will use from now onwards.

No command line arguments is passed
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
can someone please reply to this immediately.
Prafull Prashant
Greenhorn

Joined: Jan 15, 2010
Posts: 8
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:


Prafull

SCJP
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
thanks prafull,


so where should i alter the program.could to rewrite only that part and post it.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
are you compiling this program from command line or from eclipse/netbeans?
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
from netbeans i am compiling.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Neha Daga wrote:


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



SCJP 6
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
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

Joined: Jan 15, 2010
Posts: 8
Hey Neha,
How does the ide matter in this case?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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

Joined: Mar 24, 2009
Posts: 100
you mean like we have to run the program,ctrl+f6.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

How hard is it to search for command line arguments netbeans on google?? The first result is this...
 
GeeCON Prague 2014
 
subject: array index out of bound problem