Results 1 to 12 of 12
Like Tree2Likes
  • 2 Post By jim829

Thread: How to create an ArrayList method?

  1. #1
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default How to create an ArrayList method?

    Okay so here are the requirements my teacher gave us.

    Java Code:
     
    /** 
     *The method creates an array list of integers and then prompts the user
     * for an integer.  As long as the user continues to enter anything other
     * than -999, add the number to the array list. 
     *
     * @return the array list of numbers
     */
    I've attempted this several times but am still struggling. Can someone help?

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

    Default Re: How to create an ArrayList method?

    I'm sure lots of people can help. But you have to provide something to help *with*. (Writing code that meets your teacher's specification won't count as "help" for most people here. And I guess you weren't expecting that.)

    What have you tried? And what happened when you tried that?

    Also, do you understand what the instructions mean? If not, say what it is you don't understand.

  3. #3
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: How to create an ArrayList method?

    Quote Originally Posted by pbrockway2 View Post
    I'm sure lots of people can help. But you have to provide something to help *with*. (Writing code that meets your teacher's specification won't count as "help" for most people here. And I guess you weren't expecting that.)

    What have you tried? And what happened when you tried that?

    Also, do you understand what the instructions mean? If not, say what it is you don't understand.
    Im not sure what return type i should use for ArrayList. Also, I don't know how to continuously get integers from the user using System.in and and put those inegers to the ArrayList.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: How to create an ArrayList method?

    Quote Originally Posted by Mostpured View Post
    Im not sure what return type i should use for ArrayList.
    What return types have you tried? What happened when you tried them?

    Quote Originally Posted by Mostpured View Post
    Also, I don't know how to continuously get integers from the user using System.in and and put those inegers to the ArrayList.
    That's a separate question. But again, what have you tried? It's pretty hard to answer general "how do I do this" type questions other than by pointing you to google.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to create an ArrayList method?

    On occasion, this forum will inform members of rarely used and thus hard to locate classes. But some classes (such as those which use System.in as you mentioned) are well defined and described in this forum and elsewhere on the web. Clearly you learned about System.in so you could also learn how to take in user input.

    Eventually, you will do what we all do. Search the web and also just scan the available classes in the JDK. If something looks like it might work, you read about it. If not, you file it away for some future effort, and continue scanning the classes. After you do all of that and have no success you come back here and ask.

    Once you found the class, we can help you resolve any problems you may be having using it. A good place to start would be in the tutorials in my signature.

    Regards,
    Jim
    KevinWorkman and DarrylBurke like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: How to create an ArrayList method?

    Okay, my teacher doesn't want the whole application, just the method. I know this code probably isn't even close. I am just starting. Here is what I have:

    Java Code:
    public ArrayList<Integer> GetNumbers()
    {
    	ArrayList<Integer> numbers = new ArrayList<Integer>;
    		
    	Scanner scan = new Scanner(System.in);
    		
    	System.out.println("Enter an integer: ");
    	
    	int data = scan.nextInt();
    	
    	if (data != -999)
    	{	
    		numbers.add(data);
    	}
    	
    	return numbers;
    }
    Last edited by Mostpured; 01-29-2015 at 12:44 AM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to create an ArrayList method?

    Quote Originally Posted by Mostpured View Post
    Okay, my teacher doesn't want the whole application, just the method. I know this code probably isn't even close. I am just starting.
    Actually, your code is quite close. You need to return the arraylist so you need a return type. And your code only adds a single number and then returns.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: How to create an ArrayList method?

    Quote Originally Posted by jim829 View Post
    Actually, your code is quite close. You need to return the arraylist so you need a return type. And your code only adds a single number and then returns.

    Regards,
    Jim
    Oops I forgot name the method. Also I thought ArrayList<Integers> was the return type. I'm not sure

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to create an ArrayList method?

    Quote Originally Posted by Mostpured View Post
    Also I thought ArrayList<Integers> was the return type.
    You are correct! My mistake (I misread the method name). So you're even closer than I said :).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: How to create an ArrayList method?

    nice, thanks! Okay, so now the thing I'm having trouble with is the actually logic. I don't know how I could continuously add objects to the ArrayList as the user inputs them. Like you said, it's only waits for a single number and then returns.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to create an ArrayList method?

    Quote Originally Posted by Mostpured View Post
    nice, thanks! Okay, so now the thing I'm having trouble with is the actually logic. I don't know how I could continuously add objects to the ArrayList as the user inputs them. Like you said, it's only waits for a single number and then returns.
    Have you discussed loops yet? There are for loops and while loops. You can use a loop to continually add input. Then break out of the loop when the condition is met.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: How to create an ArrayList method?

    Quote Originally Posted by jim829 View Post
    Have you discussed loops yet? There are for loops and while loops. You can use a loop to continually add input. Then break out of the loop when the condition is met.

    Regards,
    Jim
    Thanks for all your help I managed to get it working with a while loop!

Similar Threads

  1. Create a function that sorts an arraylist of objects
    By Stavrosgr in forum New To Java
    Replies: 2
    Last Post: 12-03-2012, 06:33 PM
  2. Replies: 5
    Last Post: 11-07-2012, 12:31 AM
  3. Replies: 20
    Last Post: 01-03-2012, 05:15 PM
  4. Is it possible to create a 2D arraylist?
    By pahiker in forum New To Java
    Replies: 12
    Last Post: 06-15-2010, 05:49 PM
  5. Trying to create an undo function using an arraylist.
    By Spirit356 in forum New To Java
    Replies: 6
    Last Post: 04-27-2010, 07:01 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
  •