0

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);
        }
    } 
}    
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
Cuse
  • 5
  • 3

3 Answers3

2

The pseudo code seems needlessly complex.

This will suffice:

public void searchCourse(String courseName) {
    for(int i = 0; i < schedule.length; i++) {
        if(courseName.equals(schedule[i].substring(0, 6))) {
            System.out.println("Course Found at: " + i);
            return;
        }
    }

    System.out.println("Course Not Found");
}

Regardless, a correct translation would look like this:

public void searchCourse(String courseName) {    
    boolean flag = false;
    int index = 0;

    while(index < schedule.length && !flag) {
        String extract = schedule[index].substring(0,6);
        if (courseName.equals(extract)){
            flag = true;
        } else {
            index++;
        }
    }

    if (!flag){
        System.out.println("Course Not Found");
    } else {
        System.out.println("Course Found at: " + index);
    }
}  
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
1

Your code has to be similar as the pseudo-code.

  • In the pseudo-code, the while loop has two conditions, your code only has one. You use && in java to make a AND, so it becomes && ! notFound
  • In your first if condition, you have to set the flag to true. It's a simple affectation notFound = true

Btw, the flag should be found instead of notFound, but it does not change anything, except maybe the readability.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • That is what I am confused about is how to write the while loop with two conditions do you use a , or a ; to separate index – Cuse May 05 '16 at 21:47
  • I've upgrade my answer, so you have everything you need to do the two modifications of your code. – FiReTiTi May 05 '16 at 22:33
0

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 ++;
    } 
}
Sarabala
  • 354
  • 1
  • 3
  • 7
  • You missed a test in the while loop. So your code goes through all the array instead of stoping asap. – FiReTiTi May 05 '16 at 22:12
  • And the index has to be incremented only if the string is not found. So your index is going to be greater (just about 1) of what it should be. – FiReTiTi May 05 '16 at 22:32