Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    nickliutw is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Finding the data in Arraylist

    I need help with return an value from arraylist.
    Here is code:
    PHP Code:
    public static StudentArrayList findStudnet(ArrayList <StudentArrayList> record)
    		{
    			Scanner keyBd = new Scanner( System.in );
    			System.out.println("Please enter the Last Name of Student: ");
    			String k = keyBd.nextLine();
    			String q;
    			
    			for (StudentArrayList x: record )
    			{
    
    				if(x.getLname().equals(k))
    				
    				return System.out.println(x.toString());
    				
    				else
    				 
    				return System.out.println("Not in the list");
    				
    
    			}
    			
    		}
    For some reasons, it would not let return System.out.println(x.toString()); and return System.out.println("Not in the list"); How do I solve this problem? This is just the method I create to search.

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

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by nickliutw View Post
    I need help with return an value from arraylist.
    Here is code:
    PHP Code:
    public static StudentArrayList findStudnet(ArrayList <StudentArrayList> record)
    		{
    			Scanner keyBd = new Scanner( System.in );
    			System.out.println("Please enter the Last Name of Student: ");
    			String k = keyBd.nextLine();
    			String q;
    			
    			for (StudentArrayList x: record )
    			{
    
    				if(x.getLname().equals(k))
    				
    				return System.out.println(x.toString());
    				
    				else
    				 
    				return System.out.println("Not in the list");
    				
    
    			}
    			
    		}
    For some reasons, it would not let return System.out.println(x.toString()); and return System.out.println("Not in the list"); How do I solve this problem? This is just the method I create to search.
    You are trying to return System.out.println(...) which doesn't make sense. Instead shouldn't your method just return a StudentArrayList object as your method signature states that it must do:

    Java Code:
    // method signature:
    public static [color="red"][b]StudentArrayList[/b][/color] findStudnet(ArrayList <StudentArrayList> record)
    Inside the for loop, what is the current StudentArrayList of interest? (hint, you're printing out its toString representation). Instead of the System.out.println(...) just return the current StudentArrayList if has been "found".

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

    Default Re: Finding the data in Arraylist

    I don't understand. Do you mean instead of return the System.out.println(), I should just return StudentArrayList. I tried that already compiler would make StudnetArrayList and say it is error, cannot find symbol.
    PHP Code:
    		public static StudentArrayList findStudnet(ArrayList <StudentArrayList> record)
    		{
    			Scanner keyBd = new Scanner( System.in );
    			System.out.println("Please enter the Last Name of Student: ");
    			String k = keyBd.nextLine();
    			String q;
    			
    			for (StudentArrayList x: record )
    			{
    
    				if(x.getLname().equals(k))
    				
    				System.out.println(x.toString());
    				
    				else
    				 
    				System.out.println("Not in the list");
    				
    
    			}
    			return StudentArrayList();	
    		}
    I'm not sure this what you mean or not. I don't quite understand what are you trying to tell me. Sorry. Thanks again.

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

    Default Re: Finding the data in Arraylist

    Cross posted at How find the data in ArrayList? - Page 2 - Java

    One misleading thing in your code is the class name. It should be Student not StudentArrayList.

    What is in the variable x?
    Is that what you want to return?

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

    Default Re: Finding the data in Arraylist

    variable x is the value in the constructor, right?
    I want to return toString method if the data is find in the arraylist.
    If not, just print a line say it is not in the list.

    PS: the reason I post in different forum is because I want to try to heard some other people to see how they explain it and hopefully I can understand it.
    Because you are the only one who reply in my other forum.

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

    Default Re: Finding the data in Arraylist

    variable x is the value in the constructor, right?
    No, I was referring to the x in this line:
    for (Student x: record )

    I want to return toString method
    You return values not methods. Do you want to return a String that contains the value returned by the Student class's toString method?
    The method you have defined returns a Student object, not a String.

    If not, just print a line say it is not in the list.
    That is not enough, you must always return some value. null is a possible value to return if the Student is not found.

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

    Default Re: Finding the data in Arraylist

    You return values not methods. Do you want to return a String that contains the value returned by the Student class's toString method?
    The method you have defined returns a Student object, not a String.
    Yes, I want to return the String that contains the value returned by the Studnet class's toString method.

    That is not enough, you must always return some value. null is a possible value to return if the Student is not found.
    so all i put here is
    PHP Code:
    else
    			return null;
    But what if I want to display "it is not in the list"

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

    Default Re: Finding the data in Arraylist

    But what if I want to display "it is not in the list"
    You can do that also.

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

    Default Re: Finding the data in Arraylist

    But if I can't use system.out.println nor string.format, what else can I do ?

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

    Default Re: Finding the data in Arraylist

    But if I can't use system.out.println nor string.format
    What do you want to use the System.out.println for?
    There should NOT be an execution problem when you printout anything you want anywhere in your program.
    You can use it.

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

    Default Re: Finding the data in Arraylist

    I think a basic misunderstanding I see in your first post is how to return something from a method. Your method must return something, and the type of that something is defined in the method signature (what I highlighted in red in my first reply). System.out.println(...) doesn't return anything. Rather, all it does is print out a String to the console but if you look in the API, the method println(...) returns void or nothing, and so it should never be part of the return statement of a method. Also, if you want your method to return a String and not a StudentArrayList object, then you must change the method signature to reflect this. Myself, I'd have not have this method return a String or a StudentArrayList object, but rather have it return a Student object as this makes much more sense to me.

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

    Default Re: Finding the data in Arraylist

    I mean for the return value. I want return both data info and if the data is not in arraylist, dispaly a line say it is not.

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

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by nickliutw View Post
    I mean for the return value. I want return both data info and if the data is not in arraylist, dispaly a line say it is not.
    The display logic should not be part of this method. Instead you want to return the data if it is present, and return null or throw an exception if it is not found. The code calling the method can then decide how to display what it found using an if/else block.

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

    Default Re: Finding the data in Arraylist

    You mean like this to return true or false.
    PHP Code:
    if(x.getLname().equals(k))
    				
    				return true;
    				
    				else
    				 
    				return false;
    Then create another method to display the line I want.

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

    Default Re: Finding the data in Arraylist

    Does the variable x have the data you want to display?

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

    Default Re: Finding the data in Arraylist

    Quote Originally Posted by nickliutw View Post
    You mean like this to return true or false.
    PHP Code:
    if(x.getLname().equals(k))
    				
    				return true;
    				
    				else
    				 
    				return false;
    Then create another method to display the line I want.
    No. Return the data. Either the Student if found or null if not. Then let the calling method decide what to do based on the data it receives from the method.

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

    Default Re: Finding the data in Arraylist

    I'm not sure whether I do this right or not, but this is the code i came out so far
    PHP Code:
    		public static StudentArrayList findStudnet(ArrayList <StudentArrayList> record)
    		{
    			Scanner keyBd = new Scanner( System.in );
    			System.out.println("Please enter the Last Name of Student: ");
    			String k = keyBd.nextLine();
    			
    			for (StudentArrayList x: record )
    			{
    
    				if(x.getLName().equals(k))
    				
    				
    					//System.out.println(x.toString());
    					return x;
    				
    				else
    					//System.out.println("It is not in the list");
    					return null;
    				
    			}
    		}

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

    Default Re: Finding the data in Arraylist

    Have you tried your code?

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

    Default Re: Finding the data in Arraylist

    How many Student records will your code look at before it returns something?

  20. #20
    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
    Have you tried your code?
    Yes, I did. But run into compiler errors. Which it asked me to put an if after else.

    How many Student records will your code look at before it returns something?
    Depends on how many names are in the arraylist. Cause it will checks it in the arraylist to see if it match name, otherwise it execute the else statement.

Page 1 of 2 12 LastLast

Similar Threads

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