Results 1 to 4 of 4
  1. #1
    Alessandro95 is offline Member
    Join Date
    Dec 2012
    Location
    Malta
    Posts
    2
    Rep Power
    0

    Default Trying to Remove an Object from an Array List by reading an element of the object

    This is my code, for when the user chooses the third option. "Remove a Music Object."


    Trying to Remove an Object from an Array List by reading an element of the object-untitled.jpg

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Trying to Remove an Object from an Array List by reading an element of the object

    Hi Alessandro95, welcome to the forums!

    When you post code its a good idea to do so as text so that others can run the code. What you do is put the code within "code" tags. You put [code] at the start of the code and [/code] at the end. That way the forum software will preserve indentation.

    Java Code:
    for(Music item: MusicListIn)
    {
        System.out.println(// etc
        name = sc.nextLine();
    I'm not sure exactly what problem you are having with the code. But you say that you are trying to implement "Remove a Music Object" and yet you ask the user for the name of the thing to remove lots of times (because it's inside the loop), and that doesn't seem right.

  3. #3
    Alessandro95 is offline Member
    Join Date
    Dec 2012
    Location
    Malta
    Posts
    2
    Rep Power
    0

    Default Re: Trying to Remove an Object from an Array List by reading an element of the object

    Hi pbrockway2,
    I wasn't sure on how to implement this as Lists are a confusing topic for me. I was thinking of reading the list's objects and asking the user for the Music Name, and comparing that name with the getName() method for Music objects in another class. However I got stuck when implementing this since I didn't know how to access the objects and use only one of their attributes in order to retrieve their respective position so then I could remove it.

  4. #4
    ieee488 is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Trying to Remove an Object from an Array List by reading an element of the object

    Quote Originally Posted by Alessandro95 View Post
    Hi pbrockway2,
    I wasn't sure on how to implement this as Lists are a confusing topic for me. I was thinking of reading the list's objects and asking the user for the Music Name, and comparing that name with the getName() method for Music objects in another class. However I got stuck when implementing this since I didn't know how to access the objects and use only one of their attributes in order to retrieve their respective position so then I could remove it.
    As pbrockway2 wrote above, you need to ask once what the name is before your FOR loop to look for it.

Similar Threads

  1. Trouble with reading csv into object array
    By shaunstruwig in forum New To Java
    Replies: 1
    Last Post: 06-07-2012, 03:36 PM
  2. ArrayList remove an element from the list
    By ralf in forum New To Java
    Replies: 37
    Last Post: 07-12-2011, 07:08 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM
  5. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 02:26 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
  •