Hi There.
Im doing a beginner project here in my studie.
So here is the thing.
I have a txt file, where there are words in different language.
The user must quess the word in his language and if he dosen´t know the word, he should search for it.
I´ve chosen a JOptionpane with selections, so the first words in the txt file appears, but i want to get
the other word that it points to out.

I have a controller:

    public String lookup(String question){
    for (Object value : data.values()) {
    if (value.toString().startsWith(question)) {
        //options.add((String) value);
        return  //want to return the the other word from text file on same line
                (value.toString());//now it just returns the selected word
       return null; // returner null if word dosen´t exists

    private void jMenuItemLookupActionPerformed(java.awt.event.ActionEvent evt) {                                                
        //Her skal der søges på ordpar
        Collection lData = Controller.data.values();
        String var1 = (String) JOptionPane.showInputDialog(null, "Title", "Message", JOptionPane.QUESTION_MESSAGE,
        null, lData.toArray(new String[] {}), lData.toArray()[0]);
        String var2  = controller.lookup(var1);
        JOptionPane.showMessageDialog(rootPane, var2);
        System.out.println("Test" + var2);

Can someone help? In txt file it says ex.
Hund, Dog

and i want to get the last word out.

Or if someone have some tutorials on a site with selections with textfiles, just let me know.
And the code for the GUI:

2 Years
Discussion Span
Last Post by PeterSodborg

how about you read every line in your file.
use the split method and create instances of a (new) class TranslatedWord, having two members: word and translation
store each of them in a List, Map, ...


There is split, here is the insert code.

    //add to text file
    public void addLine(String line){
            pw = new PrintWriter(new FileWriter ("db.txt", true));
        catch(Exception FileNotFoundException){
            System.out.println("file not found error");

    public HashMap load(String filename){
        HashMap result = new HashMap();
            sc = new Scanner(new File(filename));
        catch(Exception FileNotFoundException){
            System.out.println("file not found");
        //split ordene i textfilen med et komma
            String[] wordPair = sc.nextLine().split(",");
            result.put(wordPair[0], wordPair[1]);
        return result;
    public void saveFile(){
        //luk vores printwriter
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.