It's my first time using multiple methods in a class and I dont have the grasp of how it works yet. Ive been rattling my brains out why the values are printing 0 instead of the increments that theyre spposed to have in the loop. Here's my code:
public static void main(String[] args)
throws FileNotFoundException{
Scanner f = new Scanner(new File("Readme.txt"));
String the="the", to="to", be="be";
int theCount=0, toCount=0, beCount=0;
while (f.hasNext()){
String words = f.next();
words = words.toLowerCase();
words = words.replace("?"," ").replace("!"," ").replace(","," ").replace("."," ").replace(";"," ").replace(":"," ").replace("'"," ").replace("\""," ");
theCount = frequency(words, the);
toCount = frequency(words, to);
beCount = frequency(words, be);
}
System.out.println(theCount);
System.out.println(toCount);
System.out.println(beCount);
}
public static int frequency(String word, String x){
int result=0, frequency=0;
if (word.contains(x)){
result = frequency++;
}
return result;
}
}
EDIT: it worked when i put the ++ before frequency (ie. ++frequency)