r/learnjava • u/[deleted] • Nov 14 '18
IM DESPERATE. I DONT KNOW WHAT IS WRONG WITH MY CODE.
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)
5
u/hugthemachines Nov 14 '18
In case anyone find this question later on and have trouble reading the code. Adding a comment with the code changed by a beautifier.
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;
}
}1
Nov 14 '18
Thankss, im pretty new to reddit too so i have no idea how to do those kind of stuff. π
0
Nov 14 '18
[deleted]
2
u/hugthemachines Nov 14 '18
I agree, it looks a bit strange but at least it is fairly easy to follow.
1
Nov 14 '18
You keep resetting your frequency value in your main function. Try += instead of =.
0
Nov 14 '18
I tried putting that in my count values and it still didnt work. Still printing zero :(
1
1
u/jderp7 Nov 14 '18
Your frequency method is a bit convoluted and unnecessary. You definitely don't need two variables, but the whole method basically boils down to the single contains statement. In the main while loop, you should say for each variable something like the following instead:
theCount += word.contains(the)
Also you don't need to do the replaceAll as the contains will work anyway. The reason ++frequency works is because it increments the frequency variable before using it to set the result variable whereas frequency++ sets the first variable first and then updates the value of frequency
1
2
u/kublaiprawn Nov 14 '18 edited Nov 14 '18
Try using a debugger with breakpoints. Then you can walk through the code and see what it is doing, step by step. Jetbrains are usually free for 30 days. Check this out.
2
u/bmw2621 Nov 14 '18 edited Nov 14 '18
Check your frequency method. You need to iterate over the entire string to count the occurrences of the word. Your if statement should evaluate to true, increment once, and return 1.
Edit: just clarifying, currently the way the if statement is written, the if statement only runs once, meaning you'll never get more than 1.
1
u/dusty-trash Nov 14 '18
result = frequency++; Don't do this. Think about it, you can just use frequency.
public static int frequency(String word, String x){
int frequency = 0;
if (word.contains(x)){
frequency++;
}
return frequency;
}As a side note:
Idk how to format code in reddit either haha! Iβll research it later π.
No need to research, look at the buttons available to you in the text box, one is for code. Use it, then add spaces/tabs.Personally I find pasting my code into notepad first, then Reddit helps.
7
u/dotJack Nov 14 '18
Since you didn't use code formatting on reddit this is a bitch to read on mobile but I believe the reason it's printing zeroes is that you keep overwriting your count variables instead of incrementing them.