Results 1 to 11 of 11
  1. #1
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default Need help adding data from an array with a variable.

    Well what I'm trying to do is, I'm storing the current day in an array, and whenever the user clicks a button it will make it go to the next day.
    I got the info I need stored in my array but whenever I try to use it to add an integer on it, it says "Array required, but java.util.arraylist found".

    Java Code:
        ArrayList playerList = new ArrayList();
    
        playerList.add(newplayer.getDay());
    
        [COLOR="red"]playerList[0] = newplayer.getDay() + 1;[/COLOR] 
        //"Array required, but java.util.arraylist found" I get this error for this code.
    
       crntdaylbl.setText(playerList[0] + "/30");
    Last edited by Fubarable; 05-21-2011 at 12:36 PM. Reason: code tags added

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    playerList[0] refers to an Array. You would use playerList.get() to access your ArrayList.

    Take a look at the API.
    ArrayList (Java Platform SE 6)
    Last edited by Dark; 05-21-2011 at 08:24 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Quote Originally Posted by Dark View Post
    playerList[0] refers to an Array. You use playerList.get() to access your ArrayList.

    Take a look at the API.
    ArrayList (Java Platform SE 6)
    I do not see playerList.get(), only newplayer.getDay().

    Quote Originally Posted by javaguy345 View Post
    Well what I'm trying to do is, I'm storing the current day in an array, and whenever the user clicks a button it will make it go to the next day.
    I got the info I need stored in my array but whenever I try to use it to add an integer on it, it says "Array required, but java.util.arraylist found".

    ArrayList playerList = new ArrayList();

    playerList.add(newplayer.getDay());

    playerList[0] = newplayer.getDay() + 1;
    //"Array required, but java.util.arraylist found" I get this error for this code.

    crntdaylbl.setText(playerList[0] + "/30");
    please use code tags.

    You use
    Java Code:
    playerList[0]
    which is for Array and not for ArrayList.

    try set() method, syntax is <ArrayList>.set(<index>, <object>);
    Java Code:
    [b]EXAMPLE:[/b]
    int indexToChange = 0;
    int newDay = newPlayer.getDay()+1;
    playerList.set(indexToChange,newDay);

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    get(int index)
    Returns the element at the specified position in this list.

    I was referring to how he was trying to access his array list. He was trying to access slot 0 in an Array that didn't exist. If he added newPlayer.getDay() to playerList then did playerList.get(0) he would get exactly what he was trying to do there. If you read his code you would know that playerList is an ArrayList of Objects and playerList.get() is within the bounds.

    I referred him to the API because he is probably learning about ArrayLists and by browsing the API he might stumble across the components he needs.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    pardon, I misunderstood your reply :(
    When you said "you use" what came to me is that he uses playerList.get() in his Original post, then I look at OP's post and he did not use playerList.get().

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    No matter, I was just offering a correction to his syntax. I edited my post to try and clear up any confusion.
    Last edited by Dark; 05-21-2011 at 08:24 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    I as well. Anyway, hopefully OP will be able to solve it.

  8. #8
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default

    I still haven't been able to solve it how do I put the value of what I get from an array into a variable like:
    int currentday = 0;
    int totalday = 0;
    currentday = playerList.get(0);
    totalday = currentday + 1;

    I get an error saying can't place Object into an Integer how do I fix this maybe I'm using the wrong kind of an array?

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

    Default

    Quote Originally Posted by javaguy345 View Post
    I still haven't been able to solve it how do I put the value of what I get from an array into a variable like:
    Again, it's not an array but an ArrayList. The difference is important which is why it has been discussed in several posts above. Have you read those posts? If you are confused by any of them, please ask for clarification before proceeding.


    Java Code:
    int currentday = 0;
    int totalday = 0;
    currentday = playerList.get(0);
    totalday = currentday + 1;
    I get an error saying can't place Object into an Integer how do I fix this maybe I'm using the wrong kind of an array?
    You are using a non-generic ArrayList which holds Objects. To make the Object an Integer, you'll have to cast it. Otherwise, if the ArrayList only holds Integers, then make it a generic one:

    Java Code:
    ArrayList<Integer> playerList = new ArrayList<Integer>();
    Also (and again), please use code tags when posting code.

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Changing your ArrayList to what Fubarable suggested at the end would take care of your problem. In Java 5.0 auto-boxing and unboxing came around to make everyone's life easier.

    Your ArrayList<Integer> will store an Integer wrapper, which when you do current day = playerList.get(0) will return the int you stored in that position.

    Does that make sense?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks a lot guys!, I now see that I was confusing Arrays and Array Lists, I thought they were the same thing? Anyway at the moment I'm not just storing Integers in the array all except one of the data in my array are integers. For what I'm trying to do would it be better to use an array or an array list? Also if I move my String from my array into a new array I would be able to make my original array a integer generic one would that be a good idea?

Similar Threads

  1. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  2. Print variable data type
    By BillyB in forum New To Java
    Replies: 1
    Last Post: 01-14-2011, 04:37 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 1
    Last Post: 11-06-2009, 06:47 AM
  5. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:10 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
  •