Results 1 to 20 of 30
Thread: Finding the data in Arraylist
- 09-05-2011, 07:17 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Finding the data in Arraylist
I need help with return an value from arraylist.
Here is code:
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.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"); } }
-
Re: Finding the data in Arraylist
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:
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".Java Code:// method signature: public static [color="red"][b]StudentArrayList[/b][/color] findStudnet(ArrayList <StudentArrayList> record)
- 09-05-2011, 07:52 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
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.
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.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(); }
- 09-05-2011, 08:03 PM #4
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?
- 09-05-2011, 08:17 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
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.
- 09-05-2011, 08:21 PM #6
Re: Finding the data in Arraylist
No, I was referring to the x in this line:variable x is the value in the constructor, right?
for (Student x: record )
You return values not methods. Do you want to return a String that contains the value returned by the Student class's toString method?I want to return toString method
The method you have defined returns a Student object, not a String.
That is not enough, you must always return some value. null is a possible value to return if the Student is not found.If not, just print a line say it is not in the list.
- 09-05-2011, 08:36 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: Finding the data in Arraylist
Yes, I want to return the String that contains the value returned by the Studnet class's 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.
so all i put here isThat is not enough, you must always return some value. null is a possible value to return if the Student is not found.
But what if I want to display "it is not in the list"PHP Code:else return null;
- 09-05-2011, 08:38 PM #8
Re: Finding the data in Arraylist
You can do that also.But what if I want to display "it is not in the list"
- 09-05-2011, 09:23 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: Finding the data in Arraylist
But if I can't use system.out.println nor string.format, what else can I do ?
- 09-05-2011, 09:27 PM #10
Re: Finding the data in Arraylist
What do you want to use the System.out.println for?But if I can't use system.out.println nor string.format
There should NOT be an execution problem when you printout anything you want anywhere in your program.
You can use it.
-
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.
- 09-05-2011, 09:36 PM #12
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
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.
-
Re: Finding the data in Arraylist
- 09-05-2011, 11:34 PM #14
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: Finding the data in Arraylist
You mean like this to return true or false.
Then create another method to display the line I want.PHP Code:if(x.getLname().equals(k)) return true; else return false;
- 09-05-2011, 11:35 PM #15
Re: Finding the data in Arraylist
Does the variable x have the data you want to display?
-
Re: Finding the data in Arraylist
- 09-08-2011, 02:45 AM #17
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
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; } }
- 09-08-2011, 02:57 AM #18
Re: Finding the data in Arraylist
Have you tried your code?
- 09-08-2011, 03:07 AM #19
Re: Finding the data in Arraylist
How many Student records will your code look at before it returns something?
- 09-08-2011, 05:35 AM #20
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: Finding the data in Arraylist
Yes, I did. But run into compiler errors. Which it asked me to put an if after else.
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.How many Student records will your code look at before it returns something?
Similar Threads
-
finding most highly repeated entry in ArrayList
By ankit1801 in forum New To JavaReplies: 4Last Post: 05-16-2011, 09:30 AM -
Finding an object of given properties in an ArrayList
By sonny in forum New To JavaReplies: 5Last Post: 05-17-2010, 01:13 PM -
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12-12-2007, 12:47 PM -
finding Operating System specific data
By FrankyDee in forum New To JavaReplies: 2Last Post: 09-28-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks