Processing Strings With Java Loops

Processing Strings With Java Loops


This program is going to take a phrase
from our user and find out how many blank spots there are
to determine how many words are in the phrase. To begin have created an integer variable called blankCount that’s set to 1. This variable will be increased each time we come across a blank spot in our phrase. At the end, I’m going to output the number that’s in blankCount to tell the user how many words are in their phrase. I’m going to process through the word using a for loop. I have created a new variable called ‘i’,
starting it at 0 and while ‘i’ is less than the length of the phrase, we’re going to continue through this for loop. Each time through, we’re going to increase ‘i’ by one. Inside my for loop, I need a way to
grab the current character we’re working with in phrase. So I’ll create a string variable called sub. ‘Sub’ will hold a piece of the phrase string from our counter variable to our counter variable plus one. This lets us pull out each piece of phrase
and determine if that spot that we are currently working with is a blank or if
it contains a character or number. And if that substring holds a blank, we’re going to increase blankCount by one. If not, we’re just gonna continue to a loop until we reach the end of our phrase length. Here’s our if statement to check to see what sub currently is each time through. If it is a blank space, we’re going to increase blankCount by one because that means that we’ve come across another word. If it’s not a blank, we’re just going to continue back up to our for loop, increase i by one, find the next string and see if it’s a blank. Finally, once we reached the end of
the phrase, our condition will change from true to false. We’ll exit out of our loop and print out our
results of how many words we have. Let’s run it and see it in action. Our for loop is working well. It came through here. It grabbed the first substring from 0 to 1. It said “is this a blank?” Nope. Ok, let’s increase it by one. And it went to the next one, the ‘u’. Then it went through again and again and again. Finally, it came across the substring here which contained a blank so it increased the blankCount to 2. It recognizes that there are now two words here and it added one to blankCount then we continued on until we reach the end of our phrase. It found a total of three words in our phrase. Sometimes you need help troubleshooting as you go through your code. You can add in System.out.println( ) statements to help you troubleshoot. I’m going to add one here so
I can see what would be in my temporary variable ‘sub’. I’ll also add one instead of my if
statement so I can see the current value in blankCount. Now if I run this again, I can see it’s pulling out each
character correctly and then here’s my space so then it prints out
the value of blankCount and then it continues on. Finally, my last System.out.println( ) that prints after the for loop has ended. So you can use these tricks if you’re not quite sure if your variables are correct, if you getting the correct substring. Maybe if I actually
put in 2 here and wanna know my sub was because I would have some sort of mistake. I see I have an error here because I actually
have a string out of range because this 9 is beyond how many characters there
are in the length. Using your substrings can help you troubleshoot when you have problems or issues. Then whenever you don’t need them anymore, you just delete them or you can comment them out and they won’t be in your program the next time through. This program was created so that you
could see how to use a loop variable counter to pull out the substring and how we can also at
if statements inside of your for loops to process data.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *