1

I am doing an edit distance with the user input. I am storing my values in array. then the edit distance will compare the user input with my array of strings. I am doing a loop that if the edit distance is more than 2 it will display invalid else valid.

The only problem I've got is that although the program is working out fine, the output is the result of all the '28' strings that I have in my array. I would like to display only invalid or valid once.

Test is my array of strings and user is - String user - the user input.

void testingLD()
{
  for (int i=0; i<test.length; i++)
  {
      if(getLevenshteinDistance(test[i],user) > 2)
      {
        println ("Invalid re-input");
      }
      else 
      {
        println ("Valid");
      }
  }
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107

1 Answers1

0

You have your print line functions inside your loop so they get printed once per iteration.

Try this.

void testingLD()
{
  boolean isValid = true; // assume true, check each and update

  // begin loop
  for (int i=0; i<test.length; i++)
  {
      if(getLevenshteinDistance(test[i],user) > 2)
      {
        isValid = false;
        break; // don't need to test the rest of the loop if we already found a false
      }
  }
  // end loop

  if(isValid){
    println("Valid");
  }else{
    println("Invalid re-input");
  }
}

Similarly you could count the number of valid int validCount = 0; validCount++ and then display stats about how many were valid, the percentage etc. Or keep an array of the invalid strings and display those as the ones that fail etc!

Wrap up: When you want to check an entire collection or array for some condition and output one answer make sure to have your output outside of the loop!

Patrick Murphy
  • 2,311
  • 14
  • 17
  • i am trying using the boolean, and although it does make sense and it should work like that.. the program is giving me invalid for all the possibilities, included those that are valid. sorry I am still new using processing. – user3157072 Jan 10 '17 at 21:50
  • Are you using the levenshtein distance formula from here? https://forum.processing.org/two/discussion/20018/levenshtein-distance-sketch – Patrick Murphy Jan 10 '17 at 21:58
  • i think my problem is that the the loops stops at 0 therefore if i input chocolate and my first word of the array is candy, it will stop there and give me invalidate, it is not comparing the user input with the whole array.. – user3157072 Jan 10 '17 at 22:27
  • So you want to check to see if the input matches any of the strings in the array? Remove the line `break;` from my code to run through the entire loop, that exits the loop when it finds one that is invalid. If you want to check to see if any of the strings are valid you can reverse the logic, assume false, switch the greater than sign to a less than or equal to in the if, and then set `isValid = true` – Patrick Murphy Jan 10 '17 at 22:34
  • thanks alot for your help !!!! i was doing a greater sign value instead less than ! thanks again.. solved :D ! – user3157072 Jan 10 '17 at 22:45