1 Replies - 357 Views - Last Post: 02 April 2013 - 10:47 AM Rate Topic: -----

#1 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Counting characters

Posted 02 April 2013 - 10:30 AM

Hi everyone, I am trying to create a program that (1) will count all of the characters in a string, including leading and trailing blank characters (2) count the characters not including in the leading and trailing blanks and (3) count all of the non blank characters. I the following code gives me the following results occur when I enter a test value of:

Enter a string: 2222 (it has four spaces and four "2" and four spaces:)
The String, 2222 ,
has 8 characters, including all leading and trailing blanks, (should be 12)
and 4 characters, not including leading and trailing blanks (this is OK)
and 4 non-blank characters (this is OK)

Enter a string:
22 22
The String, 22 22, (this has two spaces, two "2" two spaces, and then two "2")
has 8 characters, including all leading and trailing blanks, (this is correct)
and 4 characters, not including leading and trailing blanks (this is wrong, doesn't count the middle two spaces)
and 0 non-blank characters (this is wrong, there are 4 non-blank characters)

So I know my logic is wrong somewhere......any ideas???

import java.io. *;
import java.util.*;

public class CharacterCount2
{

	public static void main(String[] args) throws IOException
	{
	
		String str,noEndSpace;	//Declare Variables
		int length=0;
		int noEs=1;
		int whitespaceCount=0;
		int totalCharacter=0;
		
		
		BufferedReader dataIn = new BufferedReader(new  InputStreamReader(System.in));		//Get inputs from User
		System.out.print("Enter a string: ");
		str = dataIn.readLine();
		
		//Conversion of strings
		noEndSpace = str.trim();
		noEs = noEndSpace.length();
		
		while(str.length() > 0)  //looping when input of characters is valid
		{
			length = str.length();
			for (int i = 0; i < length; i++)  //counting spaces in string
			{
				if (Character.isWhitespace(str.charAt(i)))
					whitespaceCount++;
			}
			
			totalCharacter =  length - whitespaceCount;		 //removing spaces
			
			System.out.println("The String, "+(str)+ ",");		 //Output to screen
			System.out.println("has " +(length)+" characters, including all leading and trailing blanks,");
			System.out.println("and " +(noEs)+ " characters, not including leading and trailing blanks");
			System.out.println("and " +(totalCharacter)+ " non-blank characters\n");
			
			System.out.println("Enter a string: "); // request for input from user
			str = dataIn.readLine();
		}//close while
	
		System.out.println("Program complete."); //end of program no characters entered
	}//close main
}//close class



Is This A Good Question/Topic? 0
  • +

Replies To: Counting characters

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Counting characters

Posted 02 April 2013 - 10:47 AM

As you seems to be allowed to use the String class methods the lazy way count the space would simply be

String noSpaceStr = str.replaceAll(" ","");
int nbSpace = str.length() - noSpaceStr.length();



:)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1