Showing posts with label whitespace. Show all posts
Showing posts with label whitespace. Show all posts

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
*********************************************************

Determine character is whitespace

Complete source code below will show you, how to determine character is whitespace or not in java.

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


public class DetermineCharacterIsSpace
{
public static void main(String[]args)
{
//Create a space character using char by press spacebar once between single quote(' ')
//You can try change it's value by change it to a letter 'A' for example.
char a=' ';

//Determine created character is space or not
//using method isWhitespace from class Character.
//isWhitespace is a static method, so we don't need to create object when
//we want to use it.
if(Character.isWhitespace(a))
{
//Print (It is a whitespace) if character is a whitespace
System.out.println("It is a whitespace");
}
else
{
//Print (It is not a whitespace) if character is not a whitespace
System.out.println("It is not a whitespace");
}
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Omit all white space in String

Complete source code below will show you, how to omit all white space in a String.

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


public class OmitWhitespaceInString
{
public static void main(String[]args)
{
//Create a String with whitespace
String a=" Eliminate white space in t h i s s t r i ng ";

//Get String length that base on numbers of character include whitespace
int temp=a.length();

//Create an int variable
int numbersOfLetter=0;

//Create loop to count the numbers of Letter
for(int i=0; i<temp; i++)
{
if(Character.isLetter(a.charAt(i)))
{
numbersOfLetter++;
}
}

//Create an array base on numbers of letter that we get
char[]storeAllCharacter=new char[numbersOfLetter];

//Create an int variable
int temp3=0;

//Create loop to set each value in array
for(int i=0; i<temp; i++)
{
if(Character.isLetter(a.charAt(i)))
{
storeAllCharacter[temp3]=a.charAt(i);
temp3++;
}
}

//Get String from created char array
String resultStringAfterOmit=new String(storeAllCharacter);

//Print result
System.out.println(resultStringAfterOmit);
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

RELAXING NATURE VIDEO