I am trying to write a program which will take in a collection of strings from an input file and then write an output file which lists the given strings and their respective word counts, digits, # of upper case letters and # of lower case letters. I am running into the problem where each successive string is not only printing it's values for my given requests but the ones from all previous as well.
For example if the first string has 5 upper case letters and the second has only 3, the program will print that the second has 8 instead of 3.
I believe it may have something to do with either the brackets or possibly some sort of flush command to reset the values after each string in the given while loop?
When posting code, please UseCodeTags(←click) so that it will be readable. I added them for you this time.
As for your specific problem, it sounds like you're not resetting some counter to zero for each word.
Something like that.
I don't feel like poring over your code right now to try to find it, but hopefully now you have some idea what you're looking for.
And if you're having a hard time figuring out which set of braces is enclosing what, then you've got too much code and/or too much nesting in one method. Move some of the steps or inner nesting levels to their own methods.
And finally, if you can't figure out what's going on in your code, add a bunch of print statements telling you where you are and what various significant values are. You can compare that output to what you expect to happen, and where reality differs from expectation, that's where you start looking for your bug.
One of the most important things to learn about writing code is that most of it requires you to...NOT write code. I probably spend 90% of my time thinking about the process, and 10% writing actual java.
So, stop and think about what is going on. without even looking at your code, i would bet that you have a variable somewhere that you use to hold how many upper case letters the current string has. You probably also have a variable that holds the current string.
I would go on to bet that you have a loop that goes through each word in a list. at the beginning of each loop, you reset some variable from the old String to the new String. When you do this, you should reset your counter to zero.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop