Showing posts with label character. Show all posts
Showing posts with label character. Show all posts

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

Determine character is uppercase

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

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


public class DetermineCharacterIsUppercase
{
public static void main(String[]args)
{
//Create a uppercase character using char with value(B)
//You can try change it's value to letter(b)
char a='B';

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


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

Determine character is lowercase

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

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


public class DetermineCharacterIsLowercase
{
public static void main(String[]args)
{
//Create a lowercase character using char with value(b)
//You can try change it's value to letter(B)
char a='b';

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


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

Determine character is letter or digit

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

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


public class DetermineCharacterIsLetterOrDigit
{
public static void main(String[]args)
{
//Create a character using char with value(3)
//You can try change it's value to letter(k)
char a='3';

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


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

Determine character is letter or not

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

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


public class DetermineCharacterIsLetter
{
public static void main(String[]args)
{
//Create a character using char with value(m)
char a='m';

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


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

Determine character is digit or not

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

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


public class DetermineCharacterIsDigit
{
public static void main(String[]args)
{
//Create a character using char with value(4)
char a='4';

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


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

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

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

Set password symbol or echo character color for JPasswordField

Source code below will show you, how to set JPasswordField echo character color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JPasswordField;
import javax.swing.JFrame;

import java.awt.Color;

public class SetJPasswordFieldEchoCharacterColor
{
public static void main(String[]args)
{
//Create password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

//Create JFrame with title ( Set JPasswordField echo character color )
JFrame frame=new JFrame("Set JPasswordField echo character color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JPasswordField echo character color to color that you choose
passwordField.setForeground(color);

//Add JPasswordField into JFrame
frame.add(passwordField);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,65);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set password symbol or echo character for JPasswordField

Source code below will show you, how to set symbol that will be use in password field that build using JPasswordField. It also called echo character.

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


import javax.swing.JPasswordField;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class SetPasswordSymbol
{
public static void main(String[]args)
{
//Create a password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

//Set password symbol
//In my case i use *
//You can change for what you want.But make sure it is only one character
//This is because, method setEchoChar receive only one character
passwordField.setEchoChar('*');


//Create a JFrame with title ( Set Password Symbol )
JFrame frame=new JFrame("Set Password Symbol");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Add password field into JFrame
frame.add(passwordField);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//WIDTH : 300 pixels
//HEIGHT : 65 pixels
frame.setSize(300,65);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


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

Replace character in a sentence in java

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


public class ReplaceCharacterInASentence
{
public static void main(String[]args)
{
String a="Hellooo Woooorld";

//REPLACE ALL CHARACTER o WITH a
String b=a.replace('o','a');
System.out.println(b);



//REPLACE A CHARACTER
//REPLACE LAST o IN Hellooo WITH a

//IF YOU WANT TO REPLACE A CHARACTER, YOU MUST TAKE WORD
//THAT CONTAIN THE CHARACTER AND MAKE SURE
//THE WORD IS ONLY UNIQUE THAT CAN REPRESENT WORD
//CONTAIN THE CHARACTER
//This is why we take looo instead of ooo because
//ooo also exist in Woooorld
String c=a.replace("looo","looa");
System.out.println(c);
}
}


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

Search character or word in a sentence using java

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


public class SearchCharacterOrWordInASentence
{
public static void main(String[]args)
{
//-1 mean the word or character that you search not exist

String a="Hello World !!!";

//SEARCH CHARACTER
int b=a.indexOf('H');
if(b!=-1)
{
System.out.println("H exist");
}

//SEARCH Hello IN Hello World !!!
int c=a.indexOf("Hello");
if(c!=-1)
{
System.out.println("Hello exist");
}
}
}


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

RELAXING NATURE VIDEO