0

I'm making a program to sort a list of strings (Around 50) and they have a maximum of 10 characters. I have created 10 text boxes which will each take one character from the word. I'd like fill in boxes randomly and come out with word matches but don't know where to start. Will I need to use a sorting algorithm to do what I need? How do I make sure that the character is in the same position of all the words (IE if I had the word "Hello" and "Hi", if I filled in text box one with H it will return both since the first letter matches). I figure it has something to do with sub-string but I can quite put my finger on it.

Edit: If you have a hard time understanding what I've asked Cinnam outlined exactly what i'm looking for in the comments.

3
  • I don't understand what you want to achieve. You start by saying you have a list of 50 strings. Immediately after you talk about "the word". Which word? You would like to "come out with word matches". What does that mean? I think that once you manage to clearly express what you want your program to do, you'll have your algorithm. Commented Sep 19, 2015 at 13:25
  • 1
    If I'm understanding you correctly - you want to fill some letter positions and want all strings that match that? For example -in-i-g would match finding and binding? Commented Sep 19, 2015 at 13:29
  • Yes Cinnam that is correct, sorry having a hard time explaining Commented Sep 19, 2015 at 13:31

2 Answers 2

2

Here is a code that works fine.

You have to enter the options with "-" as blanks

Example :

I want to find the words with "s" as third position.

--s-------

Here is the full code. If you have any question, do not hesitate.

import java.util.ArrayList;
import java.util.Scanner;

public class myTest{

    public static void main(String[] args) {
        String [] myWords = {"Finding", "string", "from", "word", "list", "with", "some", "characters", "from", "the", "string"};
        String [] myChars = new String[10];
        String match;
        boolean matches;
        Scanner scan = new Scanner(System.in);
        ArrayList<Integer> indexes = new ArrayList<>();

        System.out.println("Enter the string that has to match ");
        System.out.println("Enter '-' if it doesn't matter");
        match = scan.nextLine();
        for (int i = 0 ; i < 10 ; i++){
            myChars[i] = String.valueOf(match.charAt(i));
            if (!myChars[i].equals("-")) indexes.add(i);
        }


        for (int i = 0 ; i < myWords.length ; i++){
            matches = true;
            for (int j = 0 ; j < indexes.size() ; j++){
                if (!String.valueOf(myWords[i].charAt(indexes.get(j))).equals(myChars[indexes.get(j)])){
                    matches = false;
                    break;
                }
            }
            if (matches) System.out.println(myWords[i]);
        }
    }
}

Example of an output :

Enter the string that has to match 
Enter '-' if it doesn't matter
-i--------
Finding
list
with
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example of how to build a regex from the TextField values:

    String[] textFieldValues = {"", "u", "", "t", "", "", "", "", "", ""};
    String testedWord = "turtle";

    StringBuilder sb = new StringBuilder("*.");
    boolean letterFound = false;

    for (int i = textFieldValues.length - 1; i >= 0; i--) {
        if (!textFieldValues[i].isEmpty()) {
            sb.append(textFieldValues[i]);
            letterFound = true;
        } else if (letterFound) {
            sb.append('.');
        }
    }
    String regex = sb.reverse().toString();

    System.out.println(regex);                      // .u.t.*
    System.out.println(testedWord.matches(regex));  // true

textFieldValues contains the letter values from the various TextFields. This example only tests one word, modify as needed to test your array.

See https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html for details about the regular expression pattern.

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.