0

I'm working on a program that will allow a user to use a console menu to input various things into an array.

  1. Input instances of the class student into the array which I have done and works.

  2. Create instances of course details which I have also done.

  3. 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 :)

1
  • I'm confused - it sounds like Students is an array of Student objects, but you're using the array as if it contained String names. Commented Apr 10, 2015 at 16:40

2 Answers 2

1

If you prefer to do a sequential search, you can use a for loop like this:

private void Search(){
   // Create a scanner for input, and get the name for search
   Scanner inputScanner = new Scanner(System.in);
   System.out.println("Type student name for search:");
   String studentName = inputScanner.nextLine();

   // use for loop to search array.
   for(int i = 0; i < studentArray.length; i++){
      if(studentArray[i].getName().equals(studentName)){
         // If student was found, print his details and return from this function.
         System.out.println(studentArray[i]);
         return;
      }
   }

   // If we reach this point, it means the student was never found in the for loop.
   System.out.println("Student not found.");
}

A couple things to note:

  • In your question, you're comparing a student object with a string. The difference in types alone is enough to make the binary search return false, so you will never get a match.
  • In my loop, I am assuming the array holds Student objects, so I call getName() on the student object to compare the two strings, so I will get a match if one exists.
  • Printing the student object itself will not just print values, you need to override toString() if you haven't yet.
Sign up to request clarification or add additional context in comments.

3 Comments

@JBNizet Thank you for the really vague question. I'm assuming you meant to say '.equals() is the proper way to do string comparison, because it compares the values not the reference.' If that's the case, I have edited the answer.
Hey man thanks for the answer, but im not sure what you mean by the getName() method and overriding the toString() method and how would I implement that? Sorry if this is a stupid question but I am only a beginner at coding and am a bit lost.
Hey man, I would like to use the result of this search to create a method that deletes the student that the user searches for. Any help with this would be greatly appreciated :)
0

First of all don't do the SuppressWarnings. It's a bad practice and not good when you are beginning to learn to start with bad practices.

I would use instead of the binarySearch for your case, just use the contains method in a list, like this:

     List <String> list = new ArrayList();  
     list.add("one"); 
     list.add("two"); 

     if (list.contains("two")) {
         System.out.println("true");
     }

In addition no need to sort the array : ).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.