Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
    Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
prans rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am coming with same doubt but in different way
As joe ess said i tried to print the lenth of "s" as
import java.io.*;
class cbr
{
int a,b;
void read(int x,int y)
{
a=x;
b=y;
}
void swap(cbr x1)
{
int t=x1.a;
x1.a=x1.b;
x1.b=t;
}
void display()
{
System.out.println(a+" "+b);
}
public static void main(String []s)
{
System.out.println("s has "+s.lenght+" elements");
cbr c=new cbr();
int a1=Integer.parseInt(s[0]);
int a2=Integer.parseInt(s[1]);
c.read(a1,a2);
System.out.println("Before swap");
c.display();
System.out.println("After swap");
c.swap(c);
c.display();
}
}

But i am getting error as cannot resolve symbol "s".
what to do
please suggest me as soon as possible.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Didn't I tell you to CarefullyChooseOneForum before? This is again a problem that's better suited in the beginner's forum. Just let Joe or a sheriff move this.

You might also want to UseCodeTags. It makes your code a lot better readable.
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi,

Your making a spelling mistake, its 'length'

so write like this.
System.out.printl("s has " + s.length + " elements" );

And answer to your previous question about ArrayIndexOutOfBoundsException.

Little explanation first :
You are using command line argument to get two integers in string form, which are converted into primitive integer using parseInt() method.

So for getting command line argument you must run your programe like this:
java cbr 35 89

You have pass to numbers like in above case '35' and '89' at run time. if you don't pass them, than you get an runtime exception of ArrayIndexOutOfBOundsException.

Your code is fine, just pass the integer at runtime. I think your are runing your programe like this :
java cbr

without passing any integers
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
Let's continue the discussion in this duplicate thread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!