Showing posts with label string. Show all posts
Showing posts with label string. 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
*********************************************************

Java reverse String and simplest palindrome checker

Today, i have learn something new and it's totally simple. Sometime when don't know about that, we will use our way that take a time to complete a simple task. What i learn today is, how to reverse String. The easiest way to reverse a String for example from "i love you" to "uoy evol i" is using method reverse() in StringBuffer class. It is also very useful especially when you want to create a palindrome checker program. Your program will be the shortest palindrome checker program in the world i think, hehehehe. I have also created a new one before, but it quit long. You can review it by click here. Now we will go to coding part. I will create two simple programs. Firstly to reverse "i love you" to "uoy evol i". Secondly i will create my shortest palindrome checker program that i ever made. Don't waste our time by hear what i said.

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


public class ReverseString
{
public static void main(String[]args)
{
String a="i love you";
StringBuffer b=new StringBuffer(a);

System.out.println(b.reverse());
}
}


*************************************************************
LIKE USUAL, JUST COMPILE AND EXECUTE IT
*************************************************************

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


import javax.swing.JOptionPane;

public class MySimplestPalindromeChecker
{
public static void main(String[]args)
{
String a=JOptionPane.showInputDialog(null,"What word that you want to check ?","Palindrome Checker",JOptionPane.QUESTION_MESSAGE);

if(a!=null)
{
String b=a;
StringBuffer c=new StringBuffer(b);
if(a.equals(c.reverse().toString()))
{
JOptionPane.showMessageDialog(null,"THIS IS A PALINDROME WORD");
}
else
{
JOptionPane.showMessageDialog(null,"THIS IS NOT A PALINDROME WORD");
}
}
}
}


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

Check word in String

Complete source code below will show you how to check word if exist or not in a String. It will compare a word that we want to check with all word that separate by white space in targeted String. Comparison will ignore case.

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


import java.util.StringTokenizer;

public class CheckWordInString
{
public static void main(String[]args)
{
String a="Hello World !";

/*
*Word(hello) that we want
*to check if exist
*or not in String a
*/
String b="hello";

/*
*Get all word that
*separate by white space
*in String a
*/
StringTokenizer st
=new StringTokenizer(a);

while(st.hasMoreTokens())
{
String temp=st.nextToken();

/*
*Compare with
*ignore case
*/
if(temp.equalsIgnoreCase(b))
{
System.out.println
("b exist in a");
break;
}
}
}
}


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

Get index of character from String

Complete source code below will show you, how to get index of a character from a String.

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


public class GetIndexOfCharacterFromString
{
public static void main(String[]args)
{
//Create a String
String a="ABCD";

//Create an int variable that will store index for character C
int index=a.indexOf('C');

//Print index that we get
System.out.println("Index for C is : "+index);
}
}


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

Get substring from String

Complete source code below will show you, how to get substring from a String.

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


public class GetSubstringFromString
{
public static void main(String[]args)
{
//This source code will show you how to get substring from a String
//We will get String "love" from String "I love you"
String a="I love you";

//We will use substring() method
//2=index in "I love you" for 'l'
//6=(index in "I love you" for 'e')+1
String b=a.substring(2,6);

//Print substring that we get
System.out.println(b);
}
}


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

Determine String end with specified word

Complete source code below will show you, how to determine whether a String end with specified word or not.

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


public class DetermineStringEndWithWord
{
public static void main(String[]args)
{
//First String
//You can try change it
String a="Hello";

//Second String
//You can try change it
String b="llo";

//Determine if first String end with second String or not
//If yes, program will print YES, otherwise it will print NO
if(a.endsWith(b))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}


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

Determine String start with specified word

Complete source code below will show you, how to determine whether a String start with specified word or not.

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


public class DetermineStringStartWithWord
{
public static void main(String[]args)
{
//First String
//You can try change it
String a="Hello";

//Second String
//You can try change it
String b="Hel";

//Determine if first String start with second String or not
//If yes, program will print YES, otherwise it will print NO
if(a.startsWith(b))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}


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

Why we don't import String

This is because String is from java.lang ( java.lang.String ) package. All class from java.lang package will import implicitly into your program by java compiler.

Click here to see other class in java.lang package

Combine String

Complete source code below will show you, how to combine two String using + symbol and concat() method from String class.

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


public class CombineString
{
public static void main(String[]args)
{
String a="Hello ";
String b="world";

//First Technique : Using + symbol
String c=a+b;

//Second Technique : Using concat() method from String class
String d=a.concat(b);

//Now we will print result. It should be same.
System.out.println(c);
System.out.println(d);
}
}


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

Determine character location in a String

Now, i will share with you, how to determine location of a character in a String. Ok...firstly you must know location of a character in a String is determine using 'index'. Index for first character in a String is equal to 0 and each character include white space after that will add 1.

Note : Character location = index

String a="Hello dude";

Below is a list all character in String above with it's index.

H - 0
e - 1
l - 2
l - 3
o - 4
white space - 5
d - 6
u - 7
d - 8
e - 9

Convert String to lowercase

Complete source code below will show you, how to convert a String to lowercase.

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


public class ConvertStringToLowercase
{
public static void main(String[]args)
{
//Create a String
String a="WELCOME TO JAVA2EVERYONE!";

//Convert created String to lowercase
a=a.toLowerCase();

//Print the String
System.out.println(a);
}
}


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

Convert String to uppercase

Complete source code below will show you, how to convert a String to uppercase.

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


public class ConvertStringToUppercase
{
public static void main(String[]args)
{
//Create a String
String a="Hi everybody!";

//Convert created String to uppercase
a=a.toUpperCase();

//Print the String
System.out.println(a);
}
}


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

Create mutable String

In java when you create a String, it's mean you create an immutable String. You cannot change it, after you create it. Complete source code below will show you, how to create a mutable String using StringBuffer class.

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


public class CreateMutableString
{
public static void main(String[]args)
{
//Create a mutable String
StringBuffer a=new StringBuffer("Hi everyone");

//Print the created String
System.out.println(a);
}
}


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

String tip from API

Strings are constant, their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

Get character from String

Complete source code below will show you, how to get character from a String in java.

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


public class GetCharacterFromString
{
public static void main(String[]args)
{
//Create a String that will be use to get character from it
String a="Hello!";

/**
*If you want to get character in a String,
*you can use charAt method from String class.
*Just put index of the character in it's argument.
*For your information index in String is start with
*0....so
*index 0 is for H
*index 1 is for e
*index 2 is for l
*index 3 is for l
*index 4 is for o
*index 5 is for !
**/

//Get first character in the String
char characterFirst=a.charAt(0);

//Print the first character that we get
System.out.println(characterFirst);
}
}


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

Get char array from String

Complete source code below will show you, how to get char array from a String. This char array we will store all characters in the String include white space.

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


public class GetCharArrayFromString
{
public static void main(String[]args)
{
//Create a String that we want to get character from it
//You can try other String that you want
String a="Get char array from String.........";

//Create an array from type char that will store all characters from String
//Method toCharArray() will get all characters in the String
char[]storeAllStringCharacter=a.toCharArray();

//Print number of characters in char array
System.out.println("Number of characters in char array is : "+storeAllStringCharacter.length+"\n\n");

//Print all character in char array include their index in String
System.out.println("*******************************");
System.out.println("THE CHARACTER IS : ");
System.out.println("*******************************");
System.out.println();

for(int i=0; i<storeAllStringCharacter.length; i++)
{
System.out.println("Character at index "+i+" in the String is : "+storeAllStringCharacter[i]);
}
}
}


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

Get String from char array

Complete source code below will show you, how to get String from char array.

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


public class GetStringFromCharArray
{
public static void main(String[]args)
{
//Create an array from char type that will store all character for a String
char[]allCharacter=new char[12];

//Set all value in created array
allCharacter[0]='H';
allCharacter[1]='e';
allCharacter[2]='l';
allCharacter[3]='l';
allCharacter[4]='o';
allCharacter[5]=' ';
allCharacter[6]='W';
allCharacter[7]='o';
allCharacter[8]='r';
allCharacter[9]='l';
allCharacter[10]='d';
allCharacter[11]='!';

//Get String from created char array
String resultString=new String(allCharacter);

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


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

Get numbers of character in a string

Complete source code below will show you, how to get numbers of character that contain in a String. If you compile and execute source code below, you will get numbers of character is equal to 15 not 14. This is because the whitespace in a String also count.

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


public class GetNumbersOfCharacterInAString
{
public static void main(String[]args)
{
//Create a String
//You can change to other String that you want
String a="Hello everybody";

//Get numbers of character in created String and store it in int variable
int numbersOfCharacter=a.length();

//Print result
System.out.println("Numbers of character is : "+numbersOfCharacter);
}
}


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

Compare String ignore capital or ordinary letters

Complete source code below will show you, how to compare two string with same characteristic but ignore about capital or ordinary letters. The characteristic that i mean here is, number of characters and sequence of characters. We will use method equalsIgnoreCase().

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


public class CompareStringIgnoreCapitalOrOrdinaryLetters
{
public static void main(String[]args)
{
//Create first String = aaAAA
//You can try other string that you want by change aaAAA
String firstString="aaAAA";

//Create second String = AAaaa
//You can try other string that you want by change AAaaa
String secondString="AAaaa";

//Check if first String is same or not to second String
if(firstString.equalsIgnoreCase(secondString))
{
//If first String equal to second String, it will print ( firstString is same to secondString )
System.out.println("firstString is same to secondString");
}
else
{
//If first String not equal to second String, it will print ( firstString is not same to secondString )
System.out.println("firstString is not same to secondString");
}
}
}


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

RELAXING NATURE VIDEO