Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
prans rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run the program i am getting array index out of bounds exception
at the step of "Integer.parseInt...."
and at the step, where i tried to print the length of "args"
i am getting that "args has zero elements"
and i couldn't enter the values manually at runtime
here is my program:

class Rectangle
{
int length, breadth;
void show(int x, int y)
{
length = x;
breadth = y;
}
int calculate()
{
return(length * breadth);
}
}
public class enter
{
public static void main(String[] args)
{
System.out.println("args has "+args.length+" elements");
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);
}
}
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here args[] is an array of whatever you entered at command prompt while executing the program , like

c:\>java ClassNameWithMainMethod arg0 arg1 .. argn


And it looks like an homework program , you need to find out solution yourself !
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i couldn't enter the values manually at runtime

How are you starting your program? Do you know how to enter values on a command line so they can be picked by Java and passed to your program in the String[] args array?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic