Home > Blockchain >  Text not found in .txt , but it's already there
Text not found in .txt , but it's already there

Time:01-13

I am going through Mooc.fi Java course and I can't figure how not to write String into file if the file already contains it. I tried only with one String and tried without " " (empty space), and without another string, but still it adds the string even when the file already contains it.

And translate() method doesn't find/return whole line in which it found the given word.

public class main {

    public static void main(String[] args) throws Exception {
        MindfulDictionary dict = new MindfulDictionary();
        dict.add("apina", "monkey");
        dict.add("banaani", "banana");
        dict.add("apina", "apfe");

        System.out.println( dict.translate("apina") );
        System.out.println( dict.translate("monkey") );
        System.out.println( dict.translate("programming") );
        System.out.println( dict.translate("banana") );

    }
}

public class MindfulDictionary {
    File file;
    FileWriter writer; 
    Scanner imeskanera;
    
    public MindfulDictionary() throws Exception {
        this.file  = new File("C:\\Users\\USER\\Desktop\\test.txt");
        this.imeskanera = new Scanner(this.file, "UTF-8");
        
    }
        
    public void add(String word, String translation) throws Exception {
        boolean found = false;
        while(this.imeskanera.hasNextLine()) {
            String lineFromFile = this.imeskanera.nextLine();
            if(word.contains(lineFromFile)) {
                found = true;
                break;
            }
        }
        if(!found) {
            this.writer = new FileWriter(this.file,true);
            this.writer.write(word  " "   translation  "\n");
            this.writer.close();
        }
    }
    
    public String translate(String word) throws Exception {
        
        String line = null;
        while(this.imeskanera.hasNextLine()) {
            String data = this.imeskanera.nextLine();
            if(data.contains(word)) {
                line = data;
                break;
            }
        
    }
        return line;
    }
}

CodePudding user response:

The problem is that your Scanner object has already been consumed by the add() method. You need to reopen the input stream in order to read the contents of the file. If you add

this.imeskanera = new Scanner(this.file, "UTF-8");

At the beginning of the translate() method, it should word. Which basically tell you that there is no need for Scanner to be a global field. Use it locally in each method. This is how I have explain the concept of file streams in the past:

Think about file streams (for reading and writing) logically. You cannot allow for such a stream to be "circular". Otherwise, when you try to get the "next line", there will always be a next line and you will never be able to stop reading (or writing). The stream is consumed when it reach the end, and once that is done, to go back to the beginning of the stream, you will need to open a new one; not reuse the old one.

I thought I needed to add this explanation even after the answer was accepted because I know new developer struggle with this concept and it because of that, it is necessary to explain it in detail.

With that said, your MindfulDictionary class should look like this:

public class MindfulDictionary {
    File file;
    FileWriter writer;
//    Scanner imeskanera;

    public MindfulDictionary() throws Exception {
        this.file = new File("test.txt"); // I changed the path to the file to make it work for me. You can change it back if you want to.
        file.createNewFile();

    }

    public void add(String word, String translation) throws Exception {
        Scanner imeskanera = new Scanner(this.file, "UTF-8");
        boolean found = false;
        while (imeskanera.hasNextLine()) {
            String lineFromFile = imeskanera.nextLine();
            if (word.contains(lineFromFile)) {
                found = true;
                break;
            }
        }
        if (!found) {
            this.writer = new FileWriter(this.file, true);
            this.writer.write(word   " "   translation   "\n");
            this.writer.close();
        }
        imeskanera.close();
    }

    public String translate(String word) throws Exception {
        Scanner imeskanera = new Scanner(this.file, "UTF-8");
        String line = null;
        while (imeskanera.hasNextLine()) {
            String data = imeskanera.nextLine();
            if (data.contains(word)) {
                line = data;
                break;
            }

        }
        imeskanera.close();
        return line;
    }
}

I ran your code with my modifications and now the output is

apina monkey
apina monkey
null
banaani banana
  •  Tags:  
  • Related