Reputation: 837
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.
Upvotes: 1
Views: 2566
Reputation: 785531
You can use this one-liner method:
public boolean CheckWord(String theWord, File theFile) {
return (new Scanner(theFile).useDelimiter("\\Z").next()).contains(theWord);
}
Upvotes: 1
Reputation: 1599
Since this seems a homework assignment, I won´t give you the code of the solution. You need to:
Upvotes: 1