Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by nickliutw View Post
    Depends on how many names are in the arraylist.
    Incorrect. Your code can only ever check one Object, the first one. If the first object is the one you want then it will return that object. If it is not then it returns null. It can never go around the loop a second time and check the second, third, fourth object.

  2. #22
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by Junky View Post
    Incorrect. Your code can only ever check one Object, the first one. If the first object is the one you want then it will return that object. If it is not then it returns null. It can never go around the loop a second time and check the second, third, fourth object.
    True. You need to not return so quickly. You need to put your return null outside of your for loop so that it will only return null after it has finished checking all the students. As you have it now it will check only the first thing and then return either null or x based on only that first student.

  3. #23
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by Taikei_no_Yuurei View Post
    You need to put your return null outside of your for loop
    Which has to be done anyway to remove the "missing return statement" error.

  4. #24
    nickliutw is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by Junky View Post
    Which has to be done anyway to remove the "missing return statement" error.
    But what should I return then? It is kind of back to the original question.

  5. #25
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Finding the data in Arraylist

    Did you read any of the previous posts?

    You need to return null but not inside the loop.

  6. #26
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: Finding the data in Arraylist

    Alright, so I've looked through your code a bit, and it seems like your problem is a fairly easy fix. Just remove else and put the return null outside the for loop.

    This does have a notable problem though that it will only return the very first person with that last name, instead of all people with that last name. Assuming you want the later result you'll need to make another array and put the stuff you've found in it until you've finished going through the full list, then either return that list or null.

    Edit: Actually the first one won't quite work because it is trying to return a student object and not a StudentArrayList object.
    Last edited by Taikei_no_Yuurei; 09-08-2011 at 06:18 AM.

  7. #27
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Finding the data in Arraylist

    Taikei

    You are new here. It is generally frowned upon providing fully coded solutions. It is the OP's task to write the code not yours. Feel free to provide as many hints as you want to help them arrive at the solution themselves.

  8. #28
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: Finding the data in Arraylist

    Ah, sorry, I'll keep that in mind in the future.

  9. #29
    nickliutw is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by Junky View Post
    Did you read any of the previous posts?

    You need to return null but not inside the loop.
    Um.. just a quick question. So I can return null out side the for loop. Then Create another method to do the print part since this method only return null, but I want to display the info whether data is find in arraylist or not. So i need to create another method to display. It is thought process correct?

    PS: I refresh the this page. So I didn't read whoever provided that answer. I all see when I refresh is sorry..... So plz bear with me with this method. I want to understand it. Thank you so much.

  10. #30
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: Finding the data in Arraylist

    You don't have to create another method to display stuff, but that would work. Your other option is to display it right before you return anything. If your only objective is to print out either student information or that no student was found, then you could change the method to return void (which is to say, nothing at all) and simply print the info itself instead of ever returning anything. Either way though you need to pull whatever you do when you don't find the student outside the for loop.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. finding most highly repeated entry in ArrayList
    By ankit1801 in forum New To Java
    Replies: 4
    Last Post: 05-16-2011, 09:30 AM
  2. Replies: 5
    Last Post: 05-17-2010, 01:13 PM
  3. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  4. finding Operating System specific data
    By FrankyDee in forum New To Java
    Replies: 2
    Last Post: 09-28-2007, 03:27 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
  •