0

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); 
}
aroth
  • 54,026
  • 20
  • 135
  • 176
  • 1
    Post your exception, full call trace and the part of code that it points to. – PM 77-1 Apr 09 '14 at 01:08
  • It seems like a `Map` would suit your purposes better than a `List`. – aroth Apr 09 '14 at 02:37
  • 'code' 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())); } } } and... public WordPair(String arg0){ arg0.trim(); int equalsIndex = arg0.indexOf("="); this.wordA = arg0.substring(0, equalsIndex-1); this.wordB = arg0.substring(equalsIndex+1); } – StillLearningGuy Apr 09 '14 at 08:05
  • i can't seem to get it to post the code in code format, sorry – StillLearningGuy Apr 09 '14 at 08:12

4 Answers4

0

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!

tony
  • 88
  • 8
  • wordpair_list is a linked list in which each element contains a wordA and a wordB. the linked list type is WordPair which is a class I made to contain both words. – StillLearningGuy Apr 09 '14 at 07:42
0

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?

Community
  • 1
  • 1
aroth
  • 54,026
  • 20
  • 135
  • 176
  • so, i debugged it using various sysouts, and learned that i was trying to find an element of an object in a linkedlist of objects. so when checking for the indexof the selected element, i need the index of where that word came from. any ideas? – StillLearningGuy Apr 09 '14 at 07:57
0

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.

neurite
  • 2,798
  • 20
  • 32
0

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.

yarivt
  • 106
  • 1
  • 4