1

So I am creating a sort-of dictionary. I am collecting known words in a text file and my program takes them from there. I have created a class WORD, which has 2 variables- spelling and meaning. Now i want my dictionary to have a method toWORD- so it would take in the word's spelling and create a WORD type variable with that word's spelling as a name. But java distinquishes the input from -- public WORD toWORD(String spelling) --'s "spelling" and won't put it in the name for -- WORD spelling = new WORD(spelling, meaning) --'s spelling. Instead it creates the WORD with the name "spelling" (not the value inputed). My code has alot of my native language in it, as i'm doing it right now for school, but I'm adding only the necessary parts here right now.

this is the WORD class:

    private String spelling;
    private String meaning;  

    public WORD(String spelling, String meaning){ 
        this.spelling=spelling;
        this.meaning=meaning;
    }
    public String means(){  
        return meaning;
    }
    public String spells(){  
        return spelling;
    }
    public boolean is(String smth){
        if (smth.equalsIgnoreCase(spelling)){
            return 1==1;
        }
        else{
            return 1==0;
        }
    }
    public boolean isSynonym(WORD syno){ 
        if (meaning==syno.means() ) {
            return 1==1;
        }
        else {
            return 1==0;
        }
    }
}

and this is the method from vocabulary class:

public class vocabulary {

 public WORD toWORD(String spelling) throws FileNotFoundException{
        File J= new File("vocabulary.txt");
        Scanner seeker= new Scanner(J);
        String meaning="";
        int lineNum= 0;
        while (seeker.hasNextLine()){
            String line = seeker.nextLine();
            lineNum++; //vahetab rida
            if(line.startsWith(spelling)) {
                meaning= seeker.findInLine( " - ");
            }
        }
        WORD spelling = new WORD (spelling, meaning); //this should be a WORD not named "spelling", but the content of spelling (inputed when writing: vocabulary.toWORD("car")) 
        return spelling //this should return the new word with the spelling inputed
    }
}

I am also having trouble taking the meaning from text, as I am not that familiar with scanner commands. I have done some research on that and seen that most people use another command for reading files, but as I have not learned that, I would rather not use it.

also this is the vocabulary.txt content (at the moment):

hello - most common greeting
car - a motor vehicle for transport cargo or people on roads and streets.
candle - a light source made from an easily meltable substance and a solid core

So in my main method I should be able to write WORD car = vocabulary.toWORD(car); and thereon forward I should be able to use commands like car.meaning to output the meaning.

I hope I made my question understandable. My first question here and sorry for grammar mistakes. Haven't acctually used English for almost a year now.

0 Answers0