1

I was there a problem with my code, where I need a function to check whether there is the word I'm looking for or not in a file, if there is then it will return true, and if it fails will return false.

public void MyFunction(){
    String theWord = "Im very handsome";
    File theFile = new File("/home/rams/Desktop/tes.txt");
    if(CheckWord(theWord, theFile)){
      // so I will continue my coding, while I stuck :-(
    }
}

public void CheckWord(String theWord, File theFile){
   // what the code to search a word, which the word is variable theWord, the file is variable theFile
   // return true if in file there a word
   // return false if in file there not a word
   // thanks my brother
}

Thanks for advance.

Dave Jackson
  • 837
  • 5
  • 19
  • 28
  • http://docs.oracle.com/javase/tutorial/essential/regex/ – Jared Beekman Sep 18 '13 at 18:03
  • 2
    Have you tried anything (beside asking strangers to do it for you)? – Pshemo Sep 18 '13 at 18:04
  • http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29 – ced-b Sep 18 '13 at 18:07
  • @Pshemo: Good education, Okay I still search how to read from file and the compare to links above from Mr.Jared n Mr.Ced-b. – Dave Jackson Sep 18 '13 at 18:18
  • 1
    @HaicalRams SO has already few answered questions about reading contend of file. Take a look [here](http://stackoverflow.com/q/4716503/1393766), [here](http://stackoverflow.com/q/326390/1393766) or [here](http://stackoverflow.com/q/3402735/1393766). Also String class has build in method to check if string contains some substring, but since it can give false positive results (`"something".contains("thing")` returns true even if there is no separate `"thing"` word inside) people tend to use regex with \b - [word boundaries](http://www.regular-expressions.info/wordboundaries.html). – Pshemo Sep 18 '13 at 18:29

2 Answers2

1

Since this seems a homework assignment, I won´t give you the code of the solution. You need to:

  1. Your function CheckWord must return a boolean instead of void
  2. Read the text in the file (What is simplest way to read a file into String?)
  3. Compare the text in the file with your word (http://www.vogella.com/articles/JavaRegularExpressions/article.html or http://docs.oracle.com/javase/6/docs/api/java/lang/String.html)
  4. Return true or false depending on the result of the comparison
Community
  • 1
  • 1
Evans
  • 1,589
  • 1
  • 14
  • 25
1

You can use this one-liner method:

public boolean CheckWord(String theWord, File theFile) {
    return (new Scanner(theFile).useDelimiter("\\Z").next()).contains(theWord);
}
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    solved, thanks, but this need FileNotFoundException, and I just add try and catch(FileNotFoundException). Thank you very much Sir. – Dave Jackson Sep 18 '13 at 18:39
  • You're welcome, glad that it worked out for you. Yes it needs `FileNotFoundException` to be caught. – anubhava Sep 18 '13 at 18:45