- /*
- Check if string contains valid number example.
- This example shows how to check if string contains valid number
- or not using parseDouble and parseInteger methods of
- Double and Integer wrapper classes.
- */
- public class CheckValidNumberExample {
- public static void main(String[] args) {
- String[] str = new String[]{"10.20", "123456", "12.invalid"};
- for(int i=0 ; i < str.length ; i ++)
- {
- if( str[i].indexOf(".") > 0 )
- {
- try
- {
- /*
- * To check if the number is valid decimal number, use
- * double parseDouble(String str) method of
- * Double wrapper class.
- *
- * This method throws NumberFormatException if the
- * argument string is not a valid decimal number.
- */
- Double.parseDouble(str[i]);
- System.out.println(str[i] + " is a valid decimal number");
- }
- catch(NumberFormatException nme)
- {
- System.out.println(str[i] + " is not a valid decimal number");
- }
- }
- else
- {
- try
- {
- /*
- * To check if the number is valid integer number, use
- * int parseInt(String str) method of
- * Integer wrapper class.
- *
- * This method throws NumberFormatException if the
- * argument string is not a valid integer number.
- */
- Integer.parseInt(str[i]);
- System.out.println(str[i] + " is valid integer number");
- }
- catch(NumberFormatException nme)
- {
- System.out.println(str[i] + " is not a valid integer number");
- }
- }
- }
- }
- }
- /*
- Output would be
- 10.20 is a valid decimal number
- 123456 is valid integer number
- 12.invalid is not a valid decimal number
- */
Welcome to the world of Java examples, organized by categories and Java packages. Java examples (Java sample source code) help to understand functionality of various Java classes and methods as well as various programming techniques in a simple way, which is otherwise very hard to learn by reading tutorials or Java API. So start exploring...
Tuesday, November 29, 2011
Check if string contains valid number example.
Sunday, November 27, 2011
Set Thread Name Example
- /*
- Set Thread Name Example
- This Java example shows how to set name of thread using setName method
- of Thread class.
- */
- public class SetThreadNameExample {
- public static void main(String[] args) {
- //get currently running thread object
- Thread currentThread = Thread.currentThread();
- System.out.println(currentThread);
- /*
- * To set name of thread, use
- * void setName(String threadName) method of
- * Thread class.
- */
- currentThread.setName("Set Thread Name Example");
- /*
- * To get the name of thread use,
- * String getName() method of Thread class.
- */
- System.out.println("Thread Name : "+ currentThread.getName());
- }
- }
- /*
- Output of the example would be
- Thread[main,5,main]
- Thread Name : Set Thread Name Example
- */
Pause Thread Using Sleep Method Example
- /*
- Pause Thread Using Sleep Method Example
- This Java example shows how to pause currently running thread using
- sleep method of Java Thread class.
- */
- public class PauseThreadUsingSleep {
- public static void main(String[] args) {
- /*
- * To pause execution of a thread, use
- * void sleep(int milliseconds) method of Thread class.
- *
- * This is a static method and causes the suspension of the thread
- * for specified period of time.
- *
- * Please note that, this method may throw InterruptedException.
- */
- System.out.println("Print number after pausing for 1000 milliseconds");
- try{
- for(int i=0; i< 5; i++){
- System.out.println(i);
- /*
- * This thread will pause for 1000 milliseconds after
- * printing each number.
- */
- Thread.sleep(1000);
- }
- }
- catch(InterruptedException ie){
- System.out.println("Thread interrupted !" + ie);
- }
- }
- }
- /*
- Output of this example would be
- Print number after pausing for 1000 milliseconds
- 0
- 1
- 2
- 3
- 4
- */
Get Current Thread Example
- /*
- Get Current Thread Example
- This Java example shows how to get reference of current thread using
- currentThread method of Java Thread class.
- */
- public class GetCurrentThreadExample {
- public static void main(String[] args) {
- /*
- * To get the reference of currently running thread, use
- * Thread currentThread() method of Thread class.
- *
- * This is a static method.
- */
- Thread currentThread = Thread.currentThread();
- System.out.println(currentThread);
- }
- }
- /*
- Output of the example would be
- Thread[main,5,main]
- */
Create New Thread Using Runnable Example
- /*
- Create New Thread Using Runnable Example
- This Java example shows how to create a new thread by implementing
- Java Runnable interface.
- */
- /*
- * To create a thread using Runnable, a class must implement
- * Java Runnable interface.
- */
- public class CreateThreadRunnableExample implements Runnable{
- /*
- * A class must implement run method to implement Runnable
- * interface. Signature of the run method is,
- *
- * public void run()
- *
- * Code written inside run method will constite a new thread.
- * New thread will end when run method returns.
- */
- public void run(){
- for(int i=0; i < 5; i++){
- System.out.println("Child Thread : " + i);
- try{
- Thread.sleep(50);
- }
- catch(InterruptedException ie){
- System.out.println("Child thread interrupted! " + ie);
- }
- }
- System.out.println("Child thread finished!");
- }
- public static void main(String[] args) {
- /*
- * To create new thread, use
- * Thread(Runnable thread, String threadName)
- * constructor.
- *
- */
- Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");
- /*
- * To start a particular thread, use
- * void start() method of Thread class.
- *
- * Please note that, after creation of a thread it will not start
- * running until we call start method.
- */
- t.start();
- for(int i=0; i < 5; i++){
- System.out.println("Main thread : " + i);
- try{
- Thread.sleep(100);
- }
- catch(InterruptedException ie){
- System.out.println("Child thread interrupted! " + ie);
- }
- }
- System.out.println("Main thread finished!");
- }
- }
- /*
- Typical output of this thread example would be
- Main thread : 0
- Child Thread : 0
- Child Thread : 1
- Main thread : 1
- Main thread : 2
- Child Thread : 2
- Child Thread : 3
- Main thread : 3
- Main thread : 4
- Child Thread : 4
- Child thread finished!
- Main thread finished!
- */
Java Class example.
- /*
- Java Class example.
- This Java class example describes how class is defined and being used
- in Java language.
- Syntax of defining java class is,
- <modifier> class <class-name>{
- // members and methods
- }
- */
- public class JavaClassExample{
- /*
- Syntax of defining memebers of the java class is,
- <modifier> type <name>;
- */
- private String name;
- /*
- Syntax of defining methods of the java class is,
- <modifier> <return-type> methodName(<optional-parameter-list>) <exception-list>{
- ...
- }
- */
- public void setName(String n){
- //set passed parameter as name
- name = n;
- }
- public String getName(){
- //return the set name
- return name;
- }
- //main method will be called first when program is executed
- public static void main(String args[]){
- /*
- Syntax of java object creation is,
- <class-name> object-name = new <class-constructor>;
- */
- JavaClassExample javaClassExample = new JavaClassExample();
- //set name member of this object
- javaClassExample.setName("Visitor");
- // print the name
- System.out.println("Hello " + javaClassExample.getName());
- }
- }
- /*
- OUTPUT of the above given Java Class Example would be :
- Hello Visitor
- */
Subscribe to:
Posts (Atom)
How to find the most appropriate Keywords?
🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...
-
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Jav...
-
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ ...
-
// File Name GreetingClient.java import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...