// **********************************************************
// Count.java
//
// This program reads in strings (phrases) and counts the
// number of blank characters and certain other letters
// in the phrase.
// **********************************************************

import cs1.Keyboard;

public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string

// Print a program header
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();

// Read in a string and find its length
System.out.print ("Enter a sentence or phrase: ");
phrase = Keyboard.readString();
length = phrase.length();

// Initialize counts
countBlank = 0;

// a for loop to go through the string character by character
// and count the blank spaces

// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ();
}
}


This is all i have right now. I have it all set up, but i have no clue how to count the blank spaces. All the help would be appreciated. Thanks

Recommended Answers

String str = "as as as as";
int count = 0;
int limit = str.length();
for(int i = 0; i < limit; ++i)
{
    if(Character.isWhitespace(str.charAt(i))
    {
         ++count;
    }
}
Jump to Post

All 4 Replies

String str = "as as as as";
int count = 0;
int limit = str.length();
for(int i = 0; i < limit; ++i)
{
    if(Character.isWhitespace(str.charAt(i))
    {
         ++count;
    }
}
// **********************************************************
// Count.java
//
// This program reads in strings (phrases) and counts the
// number of blank characters and certain other letters
// in the phrase.
// **********************************************************

import cs1.Keyboard;

public class Count
{
    public static void main (String[] args)
    {
    String phrase; // a string of characters
    String str = "as as as as";
    int countBlank; // the number of blanks (spaces) in the phrase
    int length; // the length of the phrase
    char ch; // an individual character in the string
    int count = 0;
    int limit = str.length();

    // Print a program header
    System.out.println ();
     System.out.println ("Character Counter");
    System.out.println ();

    // Read in a string and find its length
    System.out.print ("Enter a sentence or phrase: ");
    phrase = Keyboard.readString();
    length = phrase.length();

    // Initialize counts
    countBlank = 0;

    // a for loop to go through the string character by character
    // and count the blank spaces
    for(int i = 0; i < limit; ++i)

    {

    if(Character.isWhitespace(str.charAt(i))

         {

            ++count;

            }

           }


    // Print the results
    System.out.println ();
    System.out.println ("Number of blank spaces: " + countBlank);
    System.out.println ();
    }

That is what I currently have, but I'm still getting errors. Any help would be appreciated.

Post the errors. Just saying you have got errors doesn't help us in helping you out.

Will be nice if you can also provide cs1.Keyboard; or this would be guess work and nobody will look at it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.