Finding the data in Arraylist
I need help with return an value from arraylist.
Here is code:
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.
Re: Finding the data in Arraylist
Quote:
Originally Posted by
nickliutw
I need help with return an value from arraylist.
Here is code:
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:
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".
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.
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.
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?
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.
Re: Finding the data in Arraylist
Quote:
variable x is the value in the constructor, right?
No, I was referring to the x in this line:
for (Student x: record )
Quote:
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.
Quote:
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.
Re: Finding the data in Arraylist
Quote:
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.
Quote:
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
But what if I want to display "it is not in the list"
Re: Finding the data in Arraylist
Quote:
But what if I want to display "it is not in the list"
You can do that also.
Re: Finding the data in Arraylist
But if I can't use system.out.println nor string.format, what else can I do ?
Re: Finding the data in Arraylist
Quote:
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.
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.
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
Quote:
Originally Posted by
nickliutw
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.
Re: Finding the data in Arraylist
You mean like this to return true or false.
Code:
if(x.getLname().equals(k))
return true;
else
return false;
Then create another method to display the line I want.
Re: Finding the data in Arraylist
Does the variable x have the data you want to display?
Re: Finding the data in Arraylist
Quote:
Originally Posted by
nickliutw
You mean like this to return true or false.
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.
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 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;
}
}
Re: Finding the data in Arraylist
Have you tried your code?
Re: Finding the data in Arraylist
How many Student records will your code look at before it returns something?
Re: Finding the data in Arraylist
Quote:
Originally Posted by
Junky
Have you tried your code?
Yes, I did. But run into compiler errors. Which it asked me to put an if after else.
Quote:
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.