Showing posts with label word. Show all posts
Showing posts with label word. Show all posts

Read a word user input in java

Complete source code below, will show you how to wait and read only a word from user input in command prompt.

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


import java.util.Scanner;

public class ReadOneWordUserInput
{
public static void main(String[]args)
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your gender ?)
//Print into command prompt(Female/Male)
System.out.println("What is your gender ?\nFemale/Male");

//Wait and READ A WORD from user input after user hit 'Enter'
//Method next() in class scanner will read only first word in user input
//So, second word that seperate by space will be ignore.
String myGender=readUserInput.next();

//Print what store in myGender
System.out.println(myGender);
}
}


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

Set word wrap in JTextArea

Complete source code below will show you, how to set word wrap in JTextArea.

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


import javax.swing.*;

public class SetWordWrapInJTextArea
{
public static void main(String[]args)
{
JTextArea textArea=new JTextArea();

//Set word wrap in JTextArea
textArea.setWrapStyleWord(true);

//Set line wrap in JTextArea
textArea.setLineWrap(true);

JFrame frame=new JFrame("Set word wrap in JTextArea");

frame.add(textArea);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,500);

frame.setLocationRelativeTo(null);

frame.setVisible(true);
}
}


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

Replace word in a sentence using java

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


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

//REPLACE Hello WITH Hi
String b=a.replaceAll("Hello","Hi");
a=b;
System.out.println(a);

//REPLACE World WITH Dude
b=a.replaceAll("World","Dude");
a=b;
System.out.println(a);
}
}


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

Get word from a sentence in java

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


public class GetWordFromASentence
{
public static void main(String[]args)
{
//This source code will show you how to get word Hello from sentence Hello World

//You can use method substring(index_for_first_letter_for_word,index_for_last_letter_plus_one)

//Index start with 0

//Blank space also count.In this case, space between Hello and world take 5 for it's index

String a="Hello world!!";
String b=a.substring(0,5);

System.out.println(b);
}
}


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

How to determine a word contain or not in a sentence using java ???

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


public class DetermineStringContainSubstring
{
public static void main(String[]args)
{
String a="Welcome to java world";

String substring1="Welcome to java world";
String substring2="elc";
String substring3="Wel";
String substring4=" t";
String substring5="rld";

//CHECK MATCH WHOLE WORD
if(a.matches(substring1))
{
System.out.println("substring1 match to a");
}

//CHECK SUBSTRING NOT LOCATE AT START OR END OF STRING
if(a.indexOf(substring2)>0)
{
System.out.println("substring2 contain in a");
}

//CHECK SUBSTRING NOT LOCATE AT START OR END OF STRING
if(a.indexOf(substring4)>0)
{
System.out.println("substring4 contain in a");
}

//CHECK SUBSTRING LOCATE AT START OF STRING
if(a.startsWith(substring3))
{
System.out.println("substring3 contain in a");
}

//CHECK SUBSTRING LOCATE AT END OF STRING
if(a.endsWith(substring5))
{
System.out.println("substring5 contain in a");
}
}
}


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

RELAXING NATURE VIDEO