I'm working on a program that will allow a user to use a console menu to input various things into an array.
Input instances of the class student into the array which I have done and works.
Create instances of course details which I have also done.
And search the array for a particular students details. If a student with this name exists, it will print all of their details that are stored in the array and if not will throw up a message saying something like "Student not on Course".
I'm just assuming student names are unique as I only need to store 20 of them. I have created this method so far which doesn't seem to be working. I think I need to use a for loop instead of binarySearch but I'm not quite sure how to do that as no videos or posts seem to show how to use this loop with a String and a Scanner.
This is the method so far:
public static void Search()
{
String studentName;
@SuppressWarnings("resource")
Scanner searchScanner = new Scanner(System.in);
System.out.println ("Type Student Name to Search");
studentName = searchScanner.nextLine();
int FoundName;
Arrays.sort(Students);
FoundName = Arrays.binarySearch(Students, studentName);
if (FoundName > -1)
{
System.out.println("FoundName is" + Students[FoundName]);
} else
{
System.out.println("Student not found");
}
}
Any help with this would be greatly appreciated, you would be helping a lowly noob out very much :)
Studentsis an array of Student objects, but you're using the array as if it containedStringnames.