views:

143

answers:

5

I can't get this program to count the spaces, I have a feeling there is a logic error in the loop, but I am too inexperienced to figure it out myself. Any tips?

System.out.print("Enter a string: ");
String myString = keyboard.next();
int numBlanks = 0;

//find string length
int length = myString.length() - 1;
System.out.println("length " + length);

for(int sequence = 0; sequence >= length; ++sequence);
{

    if(myString.charAt(length)==' ')
    {
        numBlanks += 1;
        length -= length;
    }
    else 
        length -= length;

}
+7  A: 

There are few bugs I can see:

Semicolon at the end of for loop. Remove it.

Next

sequence >= length

should be

sequence <= length

and

if(myString.charAt(length)==' ')

should be

if(myString.charAt(sequence)==' ')

and you need to change length at all as you are already changing sequence.

So we get:

for(int sequence = 0; sequence <= length; ++sequence) {

    if(myString.charAt(sequence)==' ') {
        numBlanks += 1;
    }
}
codaddict
+1 but think there should be a "don't" in: and you need to change length at all
barrowc
+1  A: 

sequence >= length is spoken aloud as "sequence is greater than or equal to length." It is initialized to zero. When will it be greater than or equal to length?

Jonathan Grynspan
Any reason why I got a downvote here?
Jonathan Grynspan
A: 

First issue:

change

for(int sequence = 0; sequence >= length; ++sequence);

to

for(int sequence = 0; sequence <= length; ++sequence);  //flipped >= to <=

Second issue:

change

I am too inexperienced to figure it out myself.

to

This will be some good practice for debugging.  //int confidence should never be negative

:D

CrazyJugglerDrummer
While you're fixing the 'first issue', remember to also remove the extra semicolon at the end of the for sentence :-)
Grodriguez
A: 

This is not the right way to scan a string.

// fixed semicolon at the end of for
for(int sequence = 0; sequence >= length; ++sequence)
{

    if(myString.charAt(length)==' ')
    {
        numBlanks += 1;
        length -= length;
    }
    else 
        length -= length;

}
  1. What is this variable sequence supposed to do? Why are you incrementing it AND decrementing length?
  2. When is the condition (sequence >= length) ever going to be true?
  3. Why are you decrementing length by itself?
  4. The if-else statement (if it were right) could've been rewritten as:

    if(myString.charAt(length)==' ')
    {
        numBlanks += 1;
    }
    length -= length;
    

I would do this:

numBlanks = 0;
for (char c : myString.toCharArray())
    if (c == ' ')
        numBlanks++;

So I don't have to mess with indices.

NullUserException
+1  A: 

In addition to the errors others have pointed out, there are violations of convention here; you're calculating the length as n - 1, and then comparing <= (well, >=, but should be <=). Typically, unless there is very good reason, our for loops look like this:

for (int i = 0; i < n; i++) {
    ...
}

In your case, this would be expressed as:

for (int i = 0; i < myString.length(); i++) {
    ...
}

It is also conventional to stick with "i" as the loop variable, unless there's good reason to use another; in your case, "sequence" is just confusing.

Carl Manaster