Cuse Cuse - 7 months ago 11
Java Question

Searching an array for a course

Here is the algorithm but I am having difficulty with the decipher process
:

set flag equal to false
set index equal to 0

WHILE(index is less than number of courses in array AND flag == false)
extract substring
IF(string1.equals(string2) == true) THEN //course found
set flag to true
ELSE
increment index
END IF
END WHILE

IF flag == false THEN
display message the course name was not found
ELSE
course name found at position index
END IF


And here is my code:

public void searchCourse(String courseName)
{
int index;
String extract;
boolean notFound = false;
index = 0;
while(index < SIZE)
{
extract = schedule[index].charAt(0,6);
if (courseName == extract){

}
else {
index ++;
}
if ( notFound == false){
System.out.println("Course Not Found");
}
else{
System.out.println("Course Found at: " + index);
}
}
}

Answer

Here correct code

 public void searchCourse(String courseName)
 {
    int index;
    String extract;
    boolean notFound = false;
    index = 0;
    while(index < SIZE)
    {
        notFound = false;
        extract = schedule[index].charAt(0,6);
        if (courseName == extract){
               notFound = true;
        }        
        if ( notFound == false){
            System.out.println("Course Not Found");
        } 
        else{
            System.out.println("Course Found at: " + index);
        }
        index ++;
    } 
}