I'm creating a program that perform store data and retrieve data from the arraylist. I have no clue make the program find the data in Arraylist and display the data that user is looking for. If data is not in the list, the program would print the line say that whatever user enter is not in the list. There is the code I create:

constructor:

import java.util.Scanner;

public class StudentArrayList
{
		static int count;
		String fName;
		String lName;
		String major;
		double gpa;
		int sNumber;
	
	public StudentArrayList()
	{
	
	}
	public StudentArrayList(String fN, String lN, String mj, double ga, int sN)
	{
		fName = fN;
		lName = lN;
		major = mj;
		gpa = ga;
		sNumber = sN;
		count++;
	}
	
	public void setFName (String fN)
	{
		fName = fN;
	}
	public void setlName (String lN)
	{
		lName = lN;
	}
	public void setmajor (String mj)
	{
		major = mj;
	}
	public void setGa (double ga)
	{
		gpa = ga;
	}
	public void setSnumber (int sN)
	{
		sNumber = sN;
	}
	public void setCount (int ct)
	{
		count = ct;
	}
	public String getFname()
	{
		return fName;
	}
	public String getLname()
	{
		return lName;
	}
	public String getMajor()
	{
		return major;
	}
	public double getGpa()
	{
		return gpa;
	}
	public int getSNumber()
	{
		return sNumber;
	}
	public int getCount()
	{
		return count;
	}
	@Override
	public String toString()
	{
		return String.format("Name: %s %s. Major: %s. GPA: %.1f sNumber: %d\n", fName, lName, major, gpa, sNumber);
	}


	

}

There is the class that perform the task:

import java.util.Scanner;
import java.util.ArrayList;
public class StudentList
{	
	
	public static void pause() 
	{ 
		try 
		{ 
			System.out.print("Press <Enter> to continue..."); 
			System.in.read(); 
		} 
		catch (Exception e)
		{
			System.err.printf("Error %s%c\n",e.getMessage(),7);
		}
	}
	
	public static void main(String[] args)
	{
		ArrayList <StudentArrayList> record = new ArrayList <StudentArrayList>();
	
		Scanner keyBd = new Scanner( System.in );
		char selection;

		do{
			System.out.println("\n--------------");
			System.out.println("1. Add Student");
			System.out.println("2. Remove Studnet");
			System.out.println("3. Find Studnet");
			System.out.println("4. Display All Studnet");
			System.out.println("5. Exit\n");
			System.out.print  ("Selection: ");

			selection = keyBd.next().charAt(0);
						
			switch (selection){
				case '1':
					record.add(addStudent());
					pause();
					break;
				case '2':
					removeStudent(record);
					pause();
					break;
				case '3':
					pause();
					break;
				case '4':
					displayAllStudent(record);
					pause();
					break;
				case '5':
					break;
				default :
					
					System.out.println("Invalid Selection");
			}//end switch

		}while( selection != '5');
		
	}
		public static StudentArrayList addStudent()
	{
		Scanner keyBd = new Scanner( System.in );
		System.out.println("First Name: ");
		String fName = keyBd.nextLine();
		System.out.println("Last Name: ");
		String lName = keyBd.nextLine();
		System.out.println("Major: ");
		String major = keyBd.nextLine();
		System.out.println("gpa: ");
		double gpa = keyBd.nextDouble();
		System.out.println("S number: ");
		int sNumber = keyBd.nextInt();
		
		return new StudentArrayList(fName, lName, major,  gpa, sNumber);
	}
		public static void removeStudent(ArrayList <StudentArrayList> record)
	{
		Scanner keyBd = new Scanner( System.in );
		displayAllStudent(record);
		System.out.println("Enter the number you want to remove: ");
		int number = keyBd.nextInt();
		record.remove(number);
		
	}
	
		public static void displayAllStudent(ArrayList <StudentArrayList> record)
		{
			int d =0;
			for( StudentArrayList s: record)
			{
				
				System.out.print(d+".      "+s.toString());
				d++;
			}
		}
		
		public static StudentArrayList findStudnet(ArrayList <StudentArrayList> record)
		{
			Scanner keyBd = new Scanner( System.in );
			System.out.println("Please enter the Student Name: ");
			String k = keyBd.nextLine();


		}
	
}

I want the user type in the student name, and the program would search in arraylist and if the name is in the list, it display the name and other information. If not, that display whatever user is not in the list. I do thought about use "contains" to check to see if the data is in the list or not. But if it return true, I don't know how to make it display the other information about that student.
How do I make the program search in the arraylist and display the information is the data is in the list?

Thank You so much

You've got a lot of the code for the program but I see you are missing the code that does the find.
You would need to read in the search argument from the user and then loop through all the entries in the arraylist and compare what the user entered against what was in each object on the arraylist. You have get methods in the Student (not StudentArrayList) class that you could use to get the contents of the Student object that you want to test for a match against.

I don't quite understand. Can you give me an example? It would helps me understand if there is an example.
Thank you!

You have the code for loop in the display method.
You have the code for getting a name in the add method
All you need is to add a compare of the name obtained from the user against each Student object by calling the get name methods. Something like this:

String name = <get from user>
loop thru all objects in array list
get next Student object
if(studObj.getName().equals(name)) {
   // this object has matching name ...
}
end loop

Sorry for asking so many question. I really want to understand it. What is .equals() this method do?
Thank you again.

The equals method is defined for many classes and is used to compare the contents of the classes. Read the API doc for the String class for example

I got it works now, except I run into infinity loop which I don't know why. After it execute the command and when it hit break. It should stop and back to menu.
Here is the code:

import java.util.Scanner;
import java.util.ArrayList;
public class StudentList
{	
	
	public static void pause() 
	{ 
		try 
		{ 
			System.out.print("Press <Enter> to continue..."); 
			System.in.read(); 
		} 
		catch (Exception e)
		{
			System.err.printf("Error %s%c\n",e.getMessage(),7);
		}
	}
	
	public static void main(String[] args)
	{
		ArrayList <StudentArrayList> record = new ArrayList <StudentArrayList>();
	
		Scanner keyBd = new Scanner( System.in );
		char selection;

		do{
			System.out.println("\n--------------");
			System.out.println("1. Add Student");
			System.out.println("2. Remove Studnet");
			System.out.println("3. Find Studnet");
			System.out.println("4. Display All Studnet");
			System.out.println("5. Exit\n");
			System.out.print  ("Selection: ");

			selection = keyBd.next().charAt(0);
						
			switch (selection){
				case '1':
					record.add(addStudent());
					pause();
					break;
				case '2':
					removeStudent(record);
					pause();
					break;
				case '3':
					findStudnet(record);
					pause();
					break;
				case '4':
					displayAllStudent(record);
					pause();
					break;
				case '5':
					break;
				default :
					
					System.out.println("Invalid Selection");
			}//end switch

		}while( selection != '5');
		
	}
		public static StudentArrayList addStudent()
		{
			Scanner keyBd = new Scanner( System.in );
			System.out.println("First Name: ");
			String fName = keyBd.nextLine();
			System.out.println("Last Name: ");
			String lName = keyBd.nextLine();
			System.out.println("Major: ");
			String major = keyBd.nextLine();
			System.out.println("gpa: ");
			double gpa = keyBd.nextDouble();
			System.out.println("S number: ");
			int sNumber = keyBd.nextInt();
		
			return new StudentArrayList(fName, lName, major,  gpa, sNumber);
		}
		
		public static void removeStudent(ArrayList <StudentArrayList> record)
		{
			Scanner keyBd = new Scanner( System.in );
			displayAllStudent(record);
			System.out.println("Enter the number you want to remove: ");
			int number = keyBd.nextInt();
			record.remove(number);
		
		}
	
		public static void displayAllStudent(ArrayList <StudentArrayList> record)
		{
			int d =0;
			for( StudentArrayList s: record)
			{
				
				System.out.print(d+".      "+s.toString());
				d++;
			}
		}
		
		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());
				else
				System.out.println("Not in the list");
			}
			
			return findStudnet(record);
		}
		

	
}

run into infinity loop

Add some printlns inside of the loop to print out the values of the variables used to control the looping. Print out the values every place the variable's value changes.
The printout should show you where the problem is.

I still cannot figure where is the problem. I tried to used break, but it still continue to loop.

What was shown by the printouts? Did you print all the variables values that control the loop? Especially every time that their value was changed.

I'm not sure whether I tested it right or not.
Here is code I use to test it:

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)
System.out.println(x.toString());
else
System.out.println("Not in the list");
}
 
return findStudnet(record);
}

it just keep print whatever I type it in and loot back to "please enter the last name of the student"

What is the statement at line 16 supposed to do?

To tell the user whether the data is in arraylist or not. If it is, display the data info. If not, then display it is not in the list. I used public static, I need a return statement, right?

return the whether the data is in arraylist or not.

How do you indicate return the whether the data is in arraylist or not?
Do you return true if it is and false if it is not?

Your code does NOT return either of those values.

What does the code on line 16 do? This is line 16: return findStudnet(record);

I think I know where is the problem now. I'm return the whole method back so it would keep repeat the findstudnet method. About determine whether the data is in the array lists or not. line 9-13 would do it. If it is true, it would print the info of that studnet. if not, it would print it is not in the list. But now I run in to what should return. I just want to return whether the line 11 or line 13 depends on whether the datat is in the arraylist or not. I tried to use String.format() to do it, but it would not work. Then how would i return the line 11 or line 13

What information or data do you want to return?
true or false if the object was found
The index to the object in the list
The object from the list

When you know what that is then you can code the correct return statement.

I want to return the object from the list if it is true. If false I want it just return the println say it is not in the list.

You can't "return the println" - that makes no sense. println is a method, not an object. Your usual options would be either to return null or to throw an exception if the find fails to find anything.
ps: Remember that you can return from anywhere in a method, including inside an if test, and you can have multiple different return statements depending on what conditions you find...

The OP has given up on us and taken his problem to another forum.

I'm not really give up on you guys. I just want to try someone from other forum can explain it to me somewhat in the frequency i can understand. Because you are the only one who reply my question. I just want to try if some body else can explain it in a different way and hopefully I will understand it.

It would be polite if you posted the link to the other forums so we could see if you have the answer and then know that we don't need to do anything more to help. In other words, save us from wasting our time.

I'm so sorry. I apologized for it. I will post a link next time if I try to ask in another forum. Thank you for telling me this.

Every one will appreciate your posting links to other forums.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.