0

I am trying to show the list of words which start with the letter specified by the user input.

So for example if I add three words to my list, cat, corn and dog, and the user inputs the letter c, the output on the Java applet should be cat, corn.

However, I have no idea on how to go about this.

public void actionPerformed(ActionEvent e){
    if (e.getSource() == b1 ){
        x = textf.getText();
        wordList.add(x);
        textf.setText(null);
    } 

    if (e.getSource() == b2 ){
    }
}

b1 is adding all the user input into a secretly stored list, and I now want to make another button when pressed to show the words that start with the specified letter by the user.

textf = my text field
wordList = my list I created
x = string I previously defined 
Peter
  • 1,674
  • 4
  • 27
  • 44
JayJay Simpson
  • 155
  • 3
  • 15

3 Answers3

1

You could loop through all the possible indices, check if the element at that index starts with the letter, and print it if it does.

ALTERNATIVE (and probably better) code (I was going to put this after, but since its better it deserves to be first. Taken form @larsmans's answer here.

//given wordList as the word list
//given startChar as the character to search for in the form of a *String* not char
for (String element : wordList){
    if (element.startsWith(startChar)){
        System.out.println(element);
    }
}

DISCLAIMER: This code is untested, I don't have much experience with ArrayList, and Java is more of a quaternary programming language for me. Hope it works :)

//given same variables as before
for (int i = 0; i < wordList.size(); i++){
    String element = wordList.get(i);
    //you could remove the temporary variable and replace element with
    //  wordList.get(i)
    if (element.startsWith(startChar){
        System.out.println(element);
    }
}
Community
  • 1
  • 1
Arc676
  • 4,445
  • 3
  • 28
  • 44
  • would this actually print it out in an applet though, because thats the main problem i'm having – JayJay Simpson Nov 03 '15 at 10:59
  • Well, I don't really use applets so you would have to find that out for yourself. You could make a new text field and add the words to it. I'm pretty sure `System.out.println` works on applets. You could always just try. – Arc676 Nov 03 '15 at 11:06
  • yeah i'll just find a way because nothing prints but I know the code is right because I did something like that before but nothing happened. Thanks for your help – JayJay Simpson Nov 03 '15 at 11:07
0

You can try something like this -

public static void main(String[] args) {
        String prefix = "a";
        List<String> l = new ArrayList<String>();
        List<String> result = new ArrayList<String>();
        l.add("aah");
        l.add("abh");
        l.add("bah");

        for(String s: l) {
            if(s.startsWith(prefix)) {
                result.add(s);
            }
        }

        System.out.println(result);
   }

Result is -

[aah, abh]
Saurabh
  • 2,384
  • 6
  • 37
  • 52
0

If you can use Java 8 then you can build in features to filter your list:

public static void main(String[] args) throws Exception {
    final List<String> list = new ArrayList<>();
    list.add("cat");
    list.add("corn");
    list.add("dog");
    System.out.println(filter(list, "c"));
}

private static List<String> filter(final Collection<String> source, final String prefix) {
    return source.stream().filter(item -> item.startsWith(prefix)).collect(Collectors.toList());
}

This uses the filter method to filter each list item which starts with the String of the prefix argument.

The output is:

[cat, corn]

Tom
  • 16,842
  • 17
  • 45
  • 54