Saturday, April 24, 2010

Java count all white space in String  

0 comments

  1. import java.util.Scanner;
  2. public class CountAllWhitespaceInString
  3. {
  4. public static void main(String[]args)
  5. {
  6. boolean a=true;
  7. Scanner sc=new Scanner(System.in);
  8. while(a)
  9. {
  10. System.out.println("Type EXIT to exit");
  11. String b=sc.nextLine();
  12. int whitespaceNumber=0;
  13. if(!b.equalsIgnoreCase("exit"))
  14. {
  15. try
  16. {
  17. for(int i=0;i<=b.length()-1;i++)
  18. {
  19. char temp=b.charAt(i);
  20. if(temp==' ')
  21. {
  22. whitespaceNumber++;
  23. }
  24. }
  25. System.out.println("Whitespace number is : "+whitespaceNumber);
  26. }
  27. catch(Exception exception)
  28. {
  29. System.out.println("DON'T DO THAT");
  30. }
  31. }
  32. else
  33. {
  34. a=false;
  35. }
  36. }
  37. }
  38. }

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



Extension Factory Builder