Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Cannot search ArrayList in Java

$
0
0
I have to create an ArrayList of students which will store their first and last name, a student number, a major, and their gpa. I have the program working but I have to be able to search the array for a student based off user input and this is what I can't figure out. In class StudentList under method findStudent() I am trying to search through an ArrayList of what (I believe) is type Student and I am trying to search with a string.

I have tried many things such as simply trying to search using contains with and without loops, unsuccessfully attempted to convert the list to string via toString() and a StringBuilder. No matter how I set it up, it either won't compile or if it does, not matter what it returns false.

I attempted to comment out all the code that didn't work and erased it when it got to long figuring I would get it eventually and now realized that was a mistake as I cannot share with you all the mistaken ways I have been unable to find something in an arraylist. If anyone could point in the right direction I would greatly appreciate it.

public class Student{

	//instance variables
	private String studentFirstName;
	private String studentLastName;
	private int studentNumber;
	private String studentMajor;
	private double studentGPA;
	static int count;

	public Student( String first, String last, int number, String major, double gpa ){
		studentFirstName = first;
		studentLastName = last;		
		studentNumber = number;
		studentMajor = major;
		studentGPA = gpa;
		
		count++;
		}//end constructor
	
	public void setFirstName( String first ){
		studentFirstName = first;
	}//set studentName
	
	public String getFirstName(){
		return studentFirstName;
	}//return studentName
	
	public void setLastName( String last ){
		studentLastName = last;
	}//set studentName
	
	public String getLastName(){
		return studentLastName;
	}//return studentName

	public void setId( int number ){
		if ( number >= 0 ){
			studentNumber = number;
		}
		else
			throw new IllegalArgumentException(
				"Students number must be a positive whole number." );
	}//set studentNumber


	public int getNumber(){
		return studentNumber;
	}//return studentNumber
	
	public void setMajor( String major ){
		studentMajor = major;
	}//set studenttMajor
	
	public String getMajor(){
		return studentMajor;
	}//return studentMajor
	
	public void setGPA( double gpa ){
		if ( gpa >= 0.0 && gpa <= 4.0 ){
			studentGPA = gpa;
		}
		else
			throw new IllegalArgumentException(
				"Students GPA must be a positive number greater that or equal to 0\nand less than or equal to 4." );
	}//set studentGPA
	
	public double getGPA(){
		return studentGPA;
	}//get studentGPA
	
	@Override//overrides toString from class object
	public String toString(){
		return String.format( "        %-14s%-13s%-12d%-20s%-6.1f", getFirstName(), getLastName(), getNumber(),
		getMajor(), getGPA() );
	}//end toSting method

}//end class Student




import java.util.ArrayList;
import java.util.Scanner;

public class StudentList{
	Scanner keyBd = new Scanner( System.in );
	
	ArrayList<Student> list = new ArrayList<Student>(); //initialize ArrayList
	
	//instance variables
	private String first;
	private String last;
	private int number;
	private String major;
	private double gpa;
	private String search;
	private int arrayid;
	private char selection;
	
	public void addStudent(){		
		System.out.println("\n Add a student to list");
		System.out.print("Enter a students first name: ");
			first = keyBd.next();
		System.out.print("Enter that students last name: ");
			last = keyBd.next();
		System.out.print("Enter that students number: ");
			number = keyBd.nextInt();
		System.out.print("Enter that students Major\n(Separate words with an underscore _): ");
			major = keyBd.next();
		System.out.print("Enter that students GPA: ");
			gpa = keyBd.nextDouble();
		Student newStudent = new Student(first, last, number, major, gpa);
		
		list.add(newStudent);		
	}//end addStudent
	 
	public Student findStudent(){
		System.out.print( "Enter term to search by:\n" );
			search = keyBd.next();
		Student found=null;

		for ( Student st : list ){
			if(this.list.contains(search)){
				int index = this.list.indexOf(search);
				found = this.list.get(index);
			}	
		}		
		return found;	

	}//end findStudent

	public void removeStudent(){		
		System.out.println("\n Remove a student:");
		System.out.println("Enter the students list number you would like to remove: ");
			arrayid = keyBd.nextInt();
		if(arrayid >= 1)	{
			list.remove(arrayid-=1);
		}
	}//end removeStudent


	public void displayList(){		
		if(list.size() > 0){
			System.out.printf("%-9s%-14s%-13s%-12s%-20s%-6s\n", "Number", "First Name", "Last Name", "Student #",
				"Major", "GPA" );
			
			for(int i = 0; i < list.size(); i++){
				System.out.printf("%d%-9s\n", i+1, list.get(i));
			}
		}
		else
			System.out.println( "Student list currently empty" );	
	}//end displayList
	
	public void displaySize(){
		System.out.printf( "Total Number of Students Currently in List: %d\n", list.size() );
		System.out.printf( "Total Students Created: %d", Student.count );
	
	}//end displayList
	
	public void studentMenu(){
		char selection;//capture variable

		do{//display menu
			System.out.println("\n**************");
			System.out.println("Student List");
			System.out.println("--------------");
			System.out.println("1. Add Student");
			System.out.println("2. Find Student");
			System.out.println("3. Remove Student");
			System.out.println("4. Display List");
			System.out.println("5. Display Total Number Of Students");
			System.out.println("6. Exit\n");
			System.out.print  ("Selection: ");

			selection = keyBd.next().charAt(0);
			//get menu selection

			switch (selection){//process menu selection
				case '1':
					addStudent();
					break;
				case '2':
					findStudent();
					break;
				case '3':
					removeStudent();
					break;
				case '4':
					displayList();
					break;
				case '5':
					displaySize();
					break;
				case '6':
					break;
				default :
					System.out.println("Invalid Selection");
			}//end switch

		}while( selection != '6');//while selections is not 4, 4 ends menu which stops program
	}//end studentMenu()

}//end class StudentList



public class StudentApp{

	public static void main( String args[] ){//main declaration{
		StudentList studentList = new StudentList();
		studentList.studentMenu();
	}//end main()

}//end class StudentApp


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>