Results 1 to 16 of 16
Thread: help with length
- 02-20-2011, 08:38 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
help with length
i have this:
public class x1{
private String a;
private String b;
public void create(String a1,String b1){
this.a=a1;
this.b=b1
}
public String geta(){
return a;
}
public String getb(){
return b;
}
}
public class x2{
private int c;
private x1 d[];
public void create(){
this.c=0;
this.d= new x1[1];
}
public void add(x1 p){
if(c==d.length){
...}
}
}
public class test{
public static void main{
x2 h=new x2();
h.create();
x1 i=new x1();
String j="a";
String k="b";
i.create(j,k);
h.add(i);
}
}
trying to program this will say theres an error at the line where im using d.length, why?
-
What's the exact error message? Please post code with code tags (see my links below).
- 02-20-2011, 08:42 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
That code is riddled with errors
- 02-20-2011, 09:09 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
thats not all the program, it says arrayindexoutofbounds(but i dont think its because of the loop, thats why i didnt write )
i think that it doesnt recognizes method length for class x1(even if its not class x1, its an array of x1), i wrote System.out.println(d.length) and then told me the error was in that lineLast edited by ama91; 02-20-2011 at 09:12 PM.
- 02-20-2011, 09:33 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Can I see the code where you try to access elements of x1 array
- 02-20-2011, 10:23 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
(just in case you havent noticed yet)
i sent you all the code by private message
-
Please post it all here (with code tags), not in a private message.
- 02-20-2011, 11:19 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
For just the original question, d.length wont work because you havent given length any arguments. You should use d.length() i think, because length is a meathod.
- 02-20-2011, 11:23 PM #9
- 02-20-2011, 11:32 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Ah my mistake :)
- 02-21-2011, 12:32 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
i tryed to write it again, now theres no errors
but is throwing an exception
i will just post that part of the program
it must not enter the loop when m is 0, but is still throwing it...Java Code:public void add(x1 c) throws Exception{ boolean q=true; int i=0; while(i<m && q){ x1 per=this.d[i]; if(per.gettl0().equals(c.gettl0())){ q=false; } i++; } if(q==false){ throw new Exception ("exception3"); } }
- 02-21-2011, 12:42 AM #12
- 02-21-2011, 12:57 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Last edited by ama91; 02-21-2011 at 01:19 AM.
- 02-21-2011, 01:01 AM #14
How about instead of trying to fix your broken code you provide us with a detailed explanation of what you are trying to do. Then maybe we will be able to offer you advise that will point you in the right direction.
- 02-21-2011, 01:41 AM #15
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
i think this is useless
well, thanks for trying to help
- 02-21-2011, 01:44 AM #16
Similar Threads
-
String.length?
By rizowski in forum New To JavaReplies: 7Last Post: 11-20-2010, 05:33 PM -
string length HELP PLZ!!
By vette427 in forum EclipseReplies: 1Last Post: 09-23-2010, 06:12 AM -
length and excepton
By javagwcc in forum New To JavaReplies: 5Last Post: 06-28-2010, 11:06 PM -
what does num.length method does?
By kris09 in forum New To JavaReplies: 1Last Post: 08-07-2008, 10:19 PM -
Help with method length
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks