0

I'm attempting to write a method in a class that will prompt the user to enter the name of a student. Then search the list of already existing names for a match. But I cant seem to figure out how to proceed in coding it on how to search for a valid match.

 public void modifyExam(String [] names)
 {
 String name;
 Scanner scanner = new Scanner(System.in);
 System.out.println("Please enter the name of the student whose grade you would like to modify: ");
 name = scanner.nextLine();
 boolean nameMatch = true;
      for (int i=0; i<names.length; i++) 
      {
       // ....
      }
2
  • 1
    And what's your question? Commented Apr 20, 2014 at 20:31
  • I have no idea how to code it to search for a valid match and was hoping to get some insight. Commented Apr 20, 2014 at 20:35

5 Answers 5

2

You should use .equals() to compare strings in Java. Example:

public void modifyExam(String [] names) {
    String name;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter the name of the student whose grade you would like to modify: ");
    name = scanner.nextLine();
    boolean nameMatch = false;
    for (int i=0; i<names.length; i++) {
        if( names[i].equals(name) ) {
            // do your logic here ...
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend that you store your students in a Map with the name as the key. Then you wouldn't have to iterate (assuming your names are unique).

Map<String, Student> students = new HashMap<String, Student>();
Student s = new Student("Joe Smoe");
students.put(s.getName(), s);

Then you can lookup the student to update like this:

Student studentToUpdate = students.get(name);
if (studentToUpdate != null) {
   // logic here...
}

Note Returns null if this map contains no mapping for the key. So you would want to add a null check before using the return value of the get call and deal with it accordingly.

Comments

0

Equalities.

if(name.equals(name[i]))
    System.out.println("match at " i);

Comments

0
boolean nameMatch = false;
for (int i=0; i<names.length; i++) { 
    if(names[i].equals(name)) {
        namesMatch = true;
        break;
     }
}

The break means you don't carry on searching the array as a match has been found.

Comments

0

I would break this function into two functions. One called findName() or even more generic findString() and the other called modifyExam. Let findString() return an index, use the index in modify exam.
Here is what findString() should do

    int findString(String [] names, String name) {
        for ( int i = 0; i < names.length; i++ ) {
                if names[i].equals(name) return // either a boolean or an index or something.
        }
        return -1 // or null
   }

You could also use binary search if the search array is large and already sorted. Using binarySearch() the findString method will be something like :-

int findString(String[] names, String name, int startIndex, int stopIndex) {
    if ( startIndex > stopIndex) return;
    int mid = (stopIndex - startIndex)/2
    if ( name < names[mid] ) return findString(names, name, startIndex, mid-1); 
    else if ( names[mid].equals(name) ) return mid;
    else return findString(names, name, mid+1, stopIndex);
} 

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.