Results 1 to 16 of 16
  1. #1
    ama91 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    That code is riddled with errors

  4. #4
    ama91 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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 line
    Last edited by ama91; 02-20-2011 at 09:12 PM.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Can I see the code where you try to access elements of x1 array

  6. #6
    ama91 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    (just in case you havent noticed yet)
    i sent you all the code by private message

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  8. #8
    Dr.Fox is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Dr.Fox View Post
    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.
    No, d is an array and arrays have a length variable not a length method.

  10. #10
    Dr.Fox is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Ah my mistake :)

  11. #11
    ama91 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    i tryed to write it again, now theres no errors
    but is throwing an exception
    i will just post that part of the program

    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");
    	}
    }
    it must not enter the loop when m is 0, but is still throwing it...

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ama91 View Post
    it must not enter the loop when m is 0
    You can always add another condition.

  13. #13
    ama91 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You can always add another condition.
    why do i need another condition?
    i is 0
    m is 0
    0<0==false

    so why is still saying q!=true?
    edit:it also says q==true
    so how can it get the exception?
    maybe cause it doesnt recognize m or d again
    then im back to the first post: doesnt recognize d.length
    Last edited by ama91; 02-21-2011 at 01:19 AM.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  15. #15
    ama91 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    i think this is useless
    well, thanks for trying to help

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You get out of this forum what you put into it. If you can't be bothered providing relevant details and asking specific questions then how are we supposed to read your mind and provide you with the shiny code you obviously expect us to serve up to you.

Similar Threads

  1. String.length?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 11-20-2010, 05:33 PM
  2. string length HELP PLZ!!
    By vette427 in forum Eclipse
    Replies: 1
    Last Post: 09-23-2010, 06:12 AM
  3. length and excepton
    By javagwcc in forum New To Java
    Replies: 5
    Last Post: 06-28-2010, 11:06 PM
  4. what does num.length method does?
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 10:19 PM
  5. Help with method length
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •