Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Count Characters

    I need to add the followin to my file:

    1) The count of all characters in the string, including leading and trailing blanks.

    2) The count of characters not including leading and trailing blanks.

    3) The count of all non-blank characters.

    * Brain dead at the moment! Any assistance???

    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    public class CharacterCount
    {

    public static void main(String[] args) throws Exception
    {
    String str1, str2 = "username";
    //int index,i;
    //int initialCapacity = 5;
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    ArrayList users = new ArrayList();

    System.out.print("Enter a string: ");
    str1 = dataIn.readLine();
    str2 = dataIn.readLine();


    while(str1.length() > 0)
    {
    System.out.println("The string," +str1+ " \nhas " +str1.length()+ " characters, including all blanks, \nand "+str2.Trim()+ " characters, trimmed all leading and trailing blanks, \nand "+str1.length()+" non-blank characters.");


    System.out.print("\nEnter a user name: ");
    str1 = dataIn.readLine();
    }

    System.out.println("Program complete.");
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Since you've a string think about how to deal with each character. read about charAt() from the API and think about an algorithm you can address three steps. Give a try and let us know.

  3. #3
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    class count
    {
    public void countSpace(String S)
    {
    int i,ln,c=0;
    char ch;
    ln=S.length();
    for(i=0;i<ln;i++)
    {
    ch=S.charAt(i);
    if (S.charAt(i)==' ')
    c++;
    }
    System.out.println("number of spaces are="+c);
    Use that + the string.length() [As you already are using]

    Hope it helps

    Mjall

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Will keep it in mind sir

    Mjall

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Mjall View Post
    Will keep it in mind sir

    Mjall
    No worries, then all of us can learn something :)

  7. #7
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Still brain dead. Look up charAt, it doesnt click on. Do I need a for loop. Is it somethin simple. The code that the student post doesnt help me as well. I feel if i am missing something.

    Any other advice, I would appreciate, but dont give me the answer!!!

  8. #8
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    if (S.charAt(i)==' ')

    Basically that statements asks if your string S and its character at index I has a character that equals to (blank) and if so it increases your counter. In that way you can count your spaces.

    Mjall

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you know how many total characters there are and how many non blank characters there are how can you determine how many blank characters there are? While this will get you close, you will receive inputs
    Which will cause incorrect answers.
    Last edited by sunde887; 03-26-2011 at 10:49 AM.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by java_student88 View Post
    Still brain dead. Look up charAt, it doesnt click on. Do I need a for loop. Is it somethin simple. The code that the student post doesnt help me as well. I feel if i am missing something.

    Any other advice, I would appreciate, but dont give me the answer!!!
    Say you've a sentence. When you look at it what you are really doing is, read character by character, which is looping through it. Otherwise how you can find them.

    Loop through the string you've and check each character.

Similar Threads

  1. Count down
    By tj_wolf2 in forum New To Java
    Replies: 8
    Last Post: 03-14-2011, 06:07 PM
  2. Character Count
    By drucey in forum New To Java
    Replies: 8
    Last Post: 11-22-2010, 09:02 PM
  3. Count the characters in an input box.
    By TheRocket in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 02:25 PM
  4. Select Count
    By Apple2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 10:02 AM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •