Вопрос

I'm trying to set the text of a text field to a string based on the value selected in a JList.

  • JList = list
  • LinkedList<WordPair> = wordpair_list
  • WordPair contains wordA and wordB

If anyone can explain to me why this doesn't work I would be forever in your debt. there is obviously a lot more code in this program, but stackoverflow seems to think my text to code ratio is disproportionate. if you personally want the rest of the code I'd be glad to send it to you if you are up for the challenge.

public void showTranslation(){
    int i = wordpair_list.indexOf(list.getSelectedValue());
    textField.setText(wordpair_list.get(i).getWordB());
}

public Dictionary(Object o){ 
    if (o instanceof String){ 
        String filename = (String) o; 
        File file = new File(filename); 
        Scanner sc = null; 

        try { 
            sc = new Scanner(file); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } 

        while (sc.hasNextLine()){ 
           words.add(new WordPair(sc.nextLine())); 
        } 
    } 
}

public WordPair(String arg0) { 
    arg0.trim(); 
    int equalsIndex = arg0.indexOf("="); 
    this.wordA = arg0.substring(0, equalsIndex-1); 
    this.wordB = arg0.substring(equalsIndex+1); 
}
Это было полезно?

Решение

Your wordpair_list does not contain whatever list.getSelectedValue() returns. Note that the indexOf() method will return -1 when the specified object is not contained within the list.

You can confirm this by printing/logging the value of i.

In terms of fixing the code, if wordpair_list actually did contain list.getSelectedValue(), your code could be more concisely written as:

public void showTranslation(){
    textField.setText(list.getSelectedValue().getWordB());
}

But since that value is not in the list, you'll have to try some other approach. One possibility is that you have confused your types, and are looking in the wordpair_list for an object of an incompatible type. Another possibility is that you need to override equals() (and as a consequence, also hashCode()) so that your indexOf() lookup succeeds.

More info on that last bit here: What issues should be considered when overriding equals and hashCode in Java?

Другие советы

It's hard to tell just by the code you provided, but if wordpar_list contains only 2 elements (wordA and wordB), the output of list.getSelectedValue() can only be 0 or 1. I would try to print out (or debug) to see what list.getSelectedValue() is giving you, but it probably isn't 0 or 1 as you would expect.

Hope that helps!

This is a good case where you could use a debugger. Or insert a bunch of System.out.println() to print out some of the local values.

first you need to debug this code, print out whatever is returned from indexOf() second, you should probably not use LinkedList but rather ArrayList for a best practice.

another thing, you'll probably have to cast wordpair_list.get(i) into WordPair or whatever that object is.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top