• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Troubles Accumulating

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Im trying to create a program that counts the number of words in a provided string. When I loop through the string, I want to add +1 to the word count when there is a break between words. However, when I try to display the final word count, it keeps on returning the my initial word count.
Would anyone be able to point me in the right direction?
 
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you look at something like The String class Split method
 
Chris Sart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the quick response! However, I want it to be so any nonCharacter or Digit can be used to seperate words. For example "this-----is----four----words".
I tested the loop and it works, Im just having a hard time getting the summed wordcount to display.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch.

When faced with this type of problem the first thing to do is look at the code and work through the code as if you were the computer ie executing each line of code in your head. If there are more than a couple of variables get some paper and write out the variables names and what value is assigned to each variable then, as you mentally execute each line of code, look at your piece of paper to see what value a given variable in that line of code has and/or change the written value of any variable that is assigned a new value. You should quickly find your problem.

If you can't find it that way, the next thing is to add print statements so you can see if the value of a variable at a given point in the code is what you think it should be. In your case I suggest at the begining of the for loop you print out the value of v, aChar and bChar.
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Sart wrote:I appreciate the quick response! However, I want it to be so any nonCharacter or Digit can be used to seperate words. For example "this-----is----four----words".
I tested the loop and it works, Im just having a hard time getting the summed wordcount to display.



Have you checked if your code is entering the if statement. Put a system out message if you want saying "plus one." also the string.split method can split on anything you chose.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put in System.out.println statements to figure out what your code is really doing. for starters, i'd put on before the if-statement on line 20, and another inside the if-block, before your line 22 (note: you will need to add curly braces, which should always be there anyway for just this reason).
 
Chris Sart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured it out! Went step by step exactly as you all suggested. My if statement wasn't initializing the variables bChar and aChar so i moved the declarations into the for loop. I also had to adjust the loop control variable range.

Can someone tell me why I had to move the declarations into the loop? They weren't out of scope before right?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Sart wrote:I figured it out! Went step by step exactly as you all suggested. My if statement wasn't initializing the variables bChar and aChar so i moved the declarations into the for loop. I also had to adjust the loop control variable range.

Can someone tell me why I had to move the declarations into the loop? They weren't out of scope before right?


I suggest you go back over it again because whilst you've clearly got it working you haven't figured out what was wrong with it which suggests you've fixed it more by trial and error than by reasoning which is never a good way to approach coding/debugging.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you have a misconception on how java works.

in your original code, you have this:



let's assume that your string is "Fred".

Since v is zero, then aChar gets set to 'F', and bChar gets set to 'r'.

Now, you enter your loop. the value of v changes, but that has NO IMPACT on aChar and bChar. Those variable have been set. Java doesn't remember that aChar was set based on the variable v, so changing v does not change those.

So each time through your loop, you keep looking at the same two characters in your if-statement.
 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Sart wrote:

char aChar = newString.charAt(v);
char bChar = newString.charAt(v+1);

for (v=0; v <= newString.length();++v)
{

if (Character.isLetterOrDigit(bChar)&& !(Character.isLetterOrDigit(aChar)))
++words;


}

[/code]



The thing you are missing here:
char aChar = newString.charAt(v);
char bChar = newString.charAt(v+1);

Above code statement are not part of for loop so the values assigned only for once due to this you will never reach your if condition.
For better practice write test cases and check your code coverage.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
reply
    Bookmark Topic Watch Topic
  • New Topic