• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Adding where we dont want adding? (while loops)

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
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?

Can anyone advise on how I should proceed?


Posts: 6109
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

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.
lowercase baba
Posts: 13056
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic