what am i doing wrong here?

Discussion in 'Java' started by Jeremy Watts, Jun 4, 2005.

  1. Jeremy Watts

    Jeremy Watts Guest

    Hi,

    I've written a Java method that uses a list to return 5 three-dimensional
    arrays containing 'Big Decimal' entries. However I also want to return a
    single boolean type variable, but cant seem to get the program to do this.

    My method basically goes :-

    start method {

    declare arrays and variables
    List result = new ArrayList(5)
    ..
    ..
    ..
    ..
    REST OF METHOD
    ..
    ..
    ..
    result.add(first array)
    result.add(second array)
    etc
    result.add(fifth array)

    return (result)

    end method }


    This works fine for just the arrays, but if I attempt to add the line
    'result.add(LDMexists)' , to return the boolean variable 'LDMexists' then I
    get the error message ' cannot resolve symbol - method add(boolean)'

    I have declared the boolean type LDMexists, so I cant see what I'm doing
    wrong here - how do I get it to return the five arrays and the boolean
    variable LDMexists?

    Thanks
     
    Jeremy Watts, Jun 4, 2005
    #1
    1. Advertisements

  2. the ArrayList only allows Object references to be added, where as
    'boolean' (Note the lowercase B) is a primitive not an object.

    you can change the code to...

    result.add(Boolean.valueOf(LDMexists));

    This adds a reference to the Boolean object (note the uppercase B) to
    the arraylist.

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Boolean.html#valueOf(boolean)

    HTH

    Andrew
     
    Andrew McDonagh, Jun 4, 2005
    #2
    1. Advertisements

  3. Jeremy Watts

    Wibble Guest

    boolean is not an object, its a primative. You should wrap it in a
    Boolean instead.
     
    Wibble, Jun 4, 2005
    #3
  4. Jeremy Watts

    Jeremy Watts Guest

    Hi Andrew,

    Thanks for that, that seems to work. How would you then reference that
    variable though once the procedure has been called? I cant seem to get that
    bit to work now.

    Thanks
    Jeremy
     
    Jeremy Watts, Jun 4, 2005
    #4
  5. Jeremy Watts

    Wibble Guest

    I think that you're probably abusing ArrayList as a typeless container.
    Among other things, it forces you to typecast values when you access
    them, know the positional types or infer them, and package a primitive
    as an object Boolean.

    A better approach would be to create class to contain the
    returned object and use appropriate names and types for each member. It
    will actually run faster too since ArrayList has to do a bunch of stuff
    to support it being a growable, general purpose container.
     
    Wibble, Jun 4, 2005
    #5
  6. Jeremy Watts

    Malte Guest

    As someone else suggested you could stick a Boolean in with the arrays.
    You could also:

    1. design a composite class that would hold the arrays and the bool var
    2. have the method stick everything into a hashtable, you would then use
    the key values and casts to haul the values out of the table.
     
    Malte, Jun 4, 2005
    #6
  7. Jeremy Watts

    Jeremy Watts Guest

    how do you do that though?? sorry i'm a newbie still. the list thing that
    andrew mentioned has worked, its just i now dont know how to access the
    boolean variable once i've returned it.
     
    Jeremy Watts, Jun 4, 2005
    #7
  8. Jeremy Watts

    Wibble Guest

    public class MyClass {
    public static class MyResult {
    final int intArray[];
    final char charArray[];
    final boolean boolVal;
    public MyResult(int i[], char c[], boolean b) {
    intArray = i; charArray = c; boolVal = b;
    }
    }
    public MyResult myMethod() {
    int array1[] = new int[] { 1 , 2, 3 };
    char array2[] = "foo".toCharArray();
    boolean someBool = true;
    return(new MyResult(array1, array2, someBool));
    }
    }
     
    Wibble, Jun 4, 2005
    #8
  9. Jeremy Watts

    Jeremy Watts Guest

    managed to get that to work - it seems that the boolean value is returned as
    a string? so i needed to put quote marks around the 'true' or 'false' :)
     
    Jeremy Watts, Jun 5, 2005
    #9
  10. nope, you have mere stringified the Boolean object.

    Boolean fromTheList = (Boolean)result.get(index);

    LDMexists = fromTheList.booleanValue();


    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Boolean.html#booleanValue()
     
    Andrew McDonagh, Jun 5, 2005
    #10
  11. Jeremy Watts

    Jeremy Watts Guest

    yes ... i've just discovered this the hard way :) Your first line
    'BooleanfromTheList = (Boolean)result.get(index);' produces no errors, but
    your second line 'LDMexists = fromTheList.booleanValue();' produces the
    error "cannot resolve symbol - variable LDMexists" . whats the problem
    now??
     
    Jeremy Watts, Jun 5, 2005
    #11
  12. I thought you already had 'LDMexists' declared within your code...I was
    simply showing how you can get at the primitive boolean value from the
    Boolean Object.

    boolean primitive = fromTheList.booleanValue();
     
    Andrew McDonagh, Jun 5, 2005
    #12
  13. Jeremy Watts

    Jeremy Watts Guest

    of course... yes i had declared LDMexists in the method 'called upon', not
    in the method that was 'calling'. that all works fine now. thanks for your
    help :)
     
    Jeremy Watts, Jun 5, 2005
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.