Pages

Java count all white space in String

Yesterday, before i go to sleep i tried to answer a simple question from a site. The question is about how to count all white space from String that input by a user. After a few time i failed, i found code below :

*********************************************************
COMPLETE SOURCE CODE FOR : CountAllWhitespaceInString.java
*********************************************************


import java.util.Scanner;

public class CountAllWhitespaceInString
{
public static void main(String[]args)
{
boolean a=true;
Scanner sc=new Scanner(System.in);

while(a)
{
System.out.println("Type EXIT to exit");
String b=sc.nextLine();
int whitespaceNumber=0;

if(!b.equalsIgnoreCase("exit"))
{
try
{
for(int i=0;i<=b.length()-1;i++)
{
char temp=b.charAt(i);
if(temp==' ')
{
whitespaceNumber++;
}
}
System.out.println("Whitespace number is : "+whitespaceNumber);
}
catch(Exception exception)
{
System.out.println("DON'T DO THAT");
}
}
else
{
a=false;
}
}
}
}


*********************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You