lundi 31 août 2015

Buffer Reader code to read input file

I have a text file named "message.txt" which is read using Buffer Reader. Each line of the text file contains both "word" and "meaning" as given in this example:

"PS:Primary school"

where PS - word, Primary school - meaning

When the file is being read, each line is tokenized to "word" and "meaning" from ":". If the "meaning" is equal to the given input string called "f_msg3", "f_msg3" is displayed on the text view called "txtView". Otherwise, it displays "f_msg" on the text view.

But the "if condition" is not working properly in this code. For example if "f_msg3" is equal to "Primary school", the output on the text view must be "Primary school". But it gives the output as "f_msg" but not "f_msg3". ("f_msg3" does not contain any unnecessary strings.)

Can someone explain where I have gone wrong?

try {
    BufferedReader file = new BufferedReader(new InputStreamReader(getAssets().open("message.txt")));
    String line = "";

    while ((line = file.readLine()) != null) {
    try {
    /*separate the line into two strings at the ":" */
    StringTokenizer tokens = new StringTokenizer(line, ":"); 
    String word = tokens.nextToken();
    String meaning = tokens.nextToken();

    /*compare the given input with the meaning of the read line */
    if(meaning.equalsIgnoreCase(f_msg3)) {
    txtView.setText(f_msg3);
    } else {
    txtView.setText(f_msg);
    }
    } catch (Exception e)  {
    txtView.setText("Cannot break");
    }
    }

}   catch (IOException e) {
    txtView.setText("File not found");
}

Aucun commentaire:

Enregistrer un commentaire