Results 1 to 11 of 11
  1. #1
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default Need help with get function

    I am trying to use the get function in a vector to try to add an element in a vector to an integer. When I do this it gives me an error saying an integer and an object cannot be added together. How can I add a vector element to an integer? Thanks :)

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

    Default

    First -- what does your Vector hold? Is it Integer objects? If so, and if you have to use Java 4 or before, you'll have to get the int value of the Integer. If it's something else, you'll still need to translate it into a number before Java will allow you to add it to an int.

  3. #3
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    The vector contains integers. I am using Eclipse 3.3.2 I am not sure if that is what you are looking for. I am trying to get an integer that is in the vector at position x, then add it to another integer.

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

    Default

    The problem may be that the Vector holds Integer objects and you want to add it to a primitive int. If the Vector is not a generic Vector, in other words not declared Vector<Integer>, you will need to do the conversion yourself. Something like this may work:

    Java Code:
    int vectorInt = ((Integer)myVector.get(i)).intValue(); // cast object returned from Vector to Integer and call intValue() to convert to int primitive
    int sum = vectorInt + otherInt;

  5. #5
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    That worked. Thanks. One more quick question. If I have a two classes in different packages how can I access methods in one class from one in a different package.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    I believe you must import the class to be used, and the method to be used must be public.

  7. #7
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    I don't know how to import a class. Could you tell me how to do this?
    Thanks a bunch.

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    I assumed that you already know how to import the Vector class. You'd just do the same for the one you're trying to use. A quick Google search gave me this link: Java: Packages and Import

  9. #9
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    I think we might be thinking of different things. In my program I have created two packages: cards and participant. I need a method from a class in the cards package, in a class in the participant package. This is unrelated to the vector question I had earlier.

    Another way of wording it might be like this.

    I have class A in package X and class B in package Y. How can i make an A object in the B class?

    Thanks

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    A anA = new A(); // make an A object

    You'll need an import X.A; to get the A class definition. And you'll need the classpath set to the folder containing the X folder.

  11. #11
    calicocal is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Nevermind Thanks for the help :)
    Last edited by calicocal; 11-09-2008 at 09:11 PM. Reason: Firgured it out

Similar Threads

  1. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-08-2008, 12:33 AM
  2. Function in a variable
    By beserie in forum New To Java
    Replies: 4
    Last Post: 01-07-2008, 04:12 AM
  3. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  4. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:56 PM
  5. Help with rebound function
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:12 AM

Posting Permissions

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