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.
Thanks
And the code for the GUI:

Recommended Answers

All 2 Replies

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){
        try{
            pw = new PrintWriter(new FileWriter ("db.txt", true));
            pw.println(line);
        }
        catch(Exception FileNotFoundException){
            System.out.println("file not found error");
        }
    }

    public HashMap load(String filename){
        HashMap result = new HashMap();
        try{
            sc = new Scanner(new File(filename));
        }
        catch(Exception FileNotFoundException){
            System.out.println("file not found");
        }
        //split ordene i textfilen med et komma
        while(sc.hasNextLine()){
            String[] wordPair = sc.nextLine().split(",");
            System.out.println(wordPair[0].toString());
            System.out.println(wordPair[1].toString());
            result.put(wordPair[0], wordPair[1]);
        }
        return result;
    }
    public void saveFile(){
        //luk vores printwriter
        pw.close();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.