samedi 28 septembre 2019

How to compare text from external .txt file to a string

I have achieved returning the text from external text file by using this method here

FileReader reader = new FileReader("C:\\Users\\boung\\Desktop\\usernames.txt");

int character;

while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();

This method prints

username1

But now what I want to do is, I want to take the text that was returned from the text file and compare it to a String that was already set in the program. Like this:

        String S = "username1";

        FileReader reader = new FileReader("C:\\Users\\boung\\Desktop\\usernames.txt");
        int character;

        while ((character = reader.read()) != -1) {
            System.out.print((char) character);
        }
        reader.close();

        if (character.contains(S)) {
        System.out.println("username1 found");


    }

But I cant compare character and S.

I need to be able to stringify all of the text returned from the .txt file and check if the file contains a certain string im looking for. Can anyone help?

Aucun commentaire:

Enregistrer un commentaire