Ryan A.
Ryan A.

Reputation: 41

Displaying the number of white spaces in an inputted string?

I am trying to write a quick program that counts the number of white spaces in an inputted string. This is what I have so far:

import java.util.Scanner;

public class BlankCharacters
{
    public static void main(String[] args) 
    {   
        System.out.println("Hello, type a sentence. I will then count the number of times you use the SPACE bar.");

        String s;
        int i = 0;
        int SpaceCount = 0;

        Scanner keyboard = new Scanner(System.in);
        s = keyboard.nextLine();

        while (i != -1)
        {
            i = s.indexOf(" ");
            s = s.replace(" ", "Z");
            SpaceCount++;
        }

        System.out.println("There are " + SpaceCount + " spaces in your sentence.");     
    }
}

The while loop first uses s.indexOf(" ") to find the first white space in string s, replaces it with the char Z, then adds 1 to the value SpaceCount. This process repeats until s.indexOf does not find a white space, resulting in i being -1 and therefore stopping the loop.

In other words, SpaceCount increases by 1 every time a white space is found, which then the total number of white spaces is displayed to the user. Or it should be...

Issue: SpaceCount does not increase, instead always printing out 2.

If I were to type in "one two three four five" and print out String s, I would get "oneZtwoZthreeZfourZfive", indicating that there were four white spaces (and that the while loop runs four times). Despite that, SpaceCount remains at 2.

The program runs fine, but it always displays a SpaceCount of 2, even if the string/sentence is over ten or twenty words. I get this same result even when using a do while/for loop. I've been stuck at this for awhile and am not sure why SpaceCount is stuck at 2 when the rest of the while loop keeps executing (as intended).

Any help is much appreciated!

Upvotes: 1

Views: 238

Answers (3)

Bom Tomdabil
Bom Tomdabil

Reputation: 61

You're going the long way about counting the white spaces. Replace this block of code:

    while (i != -1)
    {
        i = s.indexOf(" ");
        s = s.replace(" ", "Z");
        SpaceCount++;
    }

With this:

char[] chars = s.toCharArray();
for(char c : chars){
    if(c == ' '){
        spaceCount++;
    }
}

This is a bit more elegant and (I believe) less expensive to execute, too. Hope that works for you!

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201447

I'm just really curious on why SpaceCount doesn't change

On the first iteration of the loop, you replace " " with nothing (all the spaces), and increment SpaceCount. On the second iteration, you find nothing (getting -1) and replace nothing and then increment SpaceCount (getting 2).

Instead of modifying the String, I would iterate the characters in the String and count the spaces.

System.out.println("Hello, type a sentence. I will then count the "
    + "number of times you use the SPACE bar.");
Scanner keyboard = new Scanner(System.in);
String s = keyboard.nextLine();
int spaceCount = 0;
for (char ch : s.toCharArray()) {
    if (ch == ' ') {
        spaceCount++;
    }
}
System.out.println("There are " + spaceCount + " spaces in your sentence.");

Also, by convention, variable names should start with a lower case letter. And, you can make the code more concise by initializing your variables when you declare them.

Upvotes: 3

sameera sy
sameera sy

Reputation: 1718

Use this, Just simple and straight forward. Replace the space character with none and subtract this with the actual length of the string. That should give the number of spaces in the string

Scanner n = new Scanner(System.in);
n.useDelimiter("\\n");
String s = n.next();
int spaceCount = s.length() - s.replaceAll(" ", "").length();
System.out.println(spaceCount);

Upvotes: 1

Related Questions