Counting blank spaces in char's


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Counting blank spaces in char's

  1. #1
    Join Date
    Feb 2006
    Posts
    8

    Unhappy Counting blank spaces in char's

    // Print the results
    System.out.println ();
    System.out.println ("Number of blank spaces: " + countBlank);
    System.out.println ();
    }
    }
    Last edited by leospyder; 02-05-2009 at 01:20 AM.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    You have the right idea... you seem to be a little confused on syntax though. = sets a value, == checks for comparison.
    Code:
    public int spaceCount(String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                count++;
            }
        }
        return count;
    }

  3. #3
    Join Date
    Feb 2006
    Posts
    8

    Unhappy

    Thanks for the help. I figured it out but now I have another problem.

    I need to:
    It would be nice to have the program let the user keep entering phrases rather than having to restart it every time. To do this we need another loop surrounding the current code. That is, the current loop will be nested inside the new loop. Add an outer while loop that will continue to execute as long as the user does NOT enter the phrase quit. Modify the prompt to tell the user to enter a phrase or quit to quit. Note that all of the initializations for the counts should be inside the while loop (that is we want the counts to start over for each new phrase entered by the user). All you need to do is add the while statement (and think about placement of your reads so the loop works correctly). Be sure to go through the program and properly indent after adding codeŚwith nested loops the inner loop should be indented.
    I added what I thought was right but when I tried initializing the case values in the while loop I got an error saying that it couldnt find the symbol.

    //////////////////////////////////////////
    import java.util.Scanner;

    public class newCount
    {
    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
    char space = ' ';
    int countA=0, countE=0, countS=0, countT=0, count_=0;

    Scanner scan = new Scanner(System.in);

    // 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 = scan.nextLine();
    length = phrase.length();

    // Initialize counts
    countBlank = 0;



    // a "for" loop to go through the string character by character and
    //count the phrase for uppercase and
    // lowercase a's, e's, s's, t's and spaces

    while (phrase.equalsIgnoreCase("quit")
    {
    }
    for (int i = 0; i<length; i++)
    {
    ch = phrase.charAt(i);
    switch (ch)
    {
    case 'a':
    case 'A':
    countA++;
    break;
    case 'e':
    case 'E':
    countE++;

    break;
    case 's':
    case 'S':
    countS++;

    break;
    case 't':
    case 'T':
    countT++;

    break;
    case ' ':
    countBlank++;
    break;

    }

    }



    // Print the results
    System.out.println ();
    System.out.println ("Number of blank spaces: " + countBlank);
    System.out.println ("There are " + countA + " a's");
    System.out.println ("There are " + countE + " e's");
    System.out.println ("There are " + countS + " s's");
    System.out.println ("There are " + countT + " t's");



    }
    }

  4. #4
    Join Date
    Apr 2006
    Posts
    28
    Just change a little bit so that program can run
    import java.util.Scanner;
    public class countBlank
    {

    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
    char space = ' ';
    int countA=0, countE=0, countS=0, countT=0, count_=0;

    Scanner scan = new Scanner(System.in);

    // 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 = scan.nextLine();
    length = phrase.length();

    // Initialize counts
    countBlank = 0;



    // a "for" loop to go through the string character by character and
    //count the phrase for uppercase and
    // lowercase a's, e's, s's, t's and spaces

    while (!phrase.equalsIgnoreCase("quit"))
    {

    for (int i = 0; i<length; i++)
    {
    phrase= phrase.toUpperCase(); //change String to uppercase
    ch = phrase.charAt(i);
    switch (ch)
    {
    case 'A':
    countA++;
    break;

    case 'E':
    countE++;
    break;

    case 'S':
    countS++;
    break;

    case 'T':
    countT++;

    break;
    case ' ':
    countBlank++;
    break;

    }


    }

    // Print the results
    System.out.println ();
    System.out.println ("Number of blank spaces: " + countBlank);
    System.out.println ("There are " + countA + " a's");
    System.out.println ("There are " + countE + " e's");
    System.out.println ("There are " + countS + " s's");
    System.out.println ("There are " + countT + " t's");

    phrase = scan.nextLine();
    length = phrase.length();
    countA=0; countE=0;
    countS=0; countT=0;
    countBlank = 0;
    }
    }
    }

Similar Threads

  1. Replies: 4
    Last Post: 02-20-2010, 04:17 AM
  2. Run-time error by a blank record made by code
    By VanDam in forum Database
    Replies: 0
    Last Post: 07-09-2005, 05:18 AM
  3. Spaces in the items in a listbox
    By Kurt in forum ASP.NET
    Replies: 2
    Last Post: 02-21-2002, 09:00 AM
  4. Disable spaces after SQL Datas
    By Alban GOBIN in forum VB Classic
    Replies: 3
    Last Post: 08-25-2000, 10:30 AM
  5. How do you search a field for blank spaces?
    By Jeff Whitehouse in forum Database
    Replies: 4
    Last Post: 05-09-2000, 05:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center