Tuesday, November 29, 2011

Check if string contains valid number example.


  1. /*
  2. Check if string contains valid number example.
  3. This example shows how to check if string contains valid number
  4. or not using parseDouble and parseInteger methods of
  5. Double and Integer wrapper classes.
  6. */
  7.  
  8. public class CheckValidNumberExample {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. String[] str = new String[]{"10.20", "123456", "12.invalid"};
  13.  
  14. for(int i=0 ; i < str.length ; i ++)
  15. {
  16.  
  17. if( str[i].indexOf(".") > 0 )
  18. {
  19.  
  20. try
  21. {
  22. /*
  23. * To check if the number is valid decimal number, use
  24. * double parseDouble(String str) method of
  25. * Double wrapper class.
  26. *
  27. * This method throws NumberFormatException if the
  28. * argument string is not a valid decimal number.
  29. */
  30. Double.parseDouble(str[i]);
  31. System.out.println(str[i] + " is a valid decimal number");
  32. }
  33. catch(NumberFormatException nme)
  34. {
  35. System.out.println(str[i] + " is not a valid decimal number");
  36. }
  37.  
  38. }
  39. else
  40. {
  41. try
  42. {
  43. /*
  44. * To check if the number is valid integer number, use
  45. * int parseInt(String str) method of
  46. * Integer wrapper class.
  47. *
  48. * This method throws NumberFormatException if the
  49. * argument string is not a valid integer number.
  50. */
  51.  
  52. Integer.parseInt(str[i]);
  53. System.out.println(str[i] + " is valid integer number");
  54. }
  55. catch(NumberFormatException nme)
  56. {
  57. System.out.println(str[i] + " is not a valid integer number");
  58. }
  59. }
  60. }
  61.  
  62. }
  63. }
  64.  
  65. /*
  66. Output would be
  67. 10.20 is a valid decimal number
  68. 123456 is valid integer number
  69. 12.invalid is not a valid decimal number
  70. */

Sunday, November 27, 2011

Set Thread Name Example


  1. /*
  2. Set Thread Name Example
  3. This Java example shows how to set name of thread using setName method
  4. of Thread class.
  5. */
  6.  
  7. public class SetThreadNameExample {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. //get currently running thread object
  12. Thread currentThread = Thread.currentThread();
  13. System.out.println(currentThread);
  14.  
  15. /*
  16. * To set name of thread, use
  17. * void setName(String threadName) method of
  18. * Thread class.
  19. */
  20.  
  21. currentThread.setName("Set Thread Name Example");
  22.  
  23. /*
  24. * To get the name of thread use,
  25. * String getName() method of Thread class.
  26. */
  27. System.out.println("Thread Name : "+ currentThread.getName());
  28. }
  29. }
  30.  
  31. /*
  32. Output of the example would be
  33. Thread[main,5,main]
  34. Thread Name : Set Thread Name Example
  35. */

Pause Thread Using Sleep Method Example


  1. /*
  2.   Pause Thread Using Sleep Method Example
  3.   This Java example shows how to pause currently running thread using
  4.   sleep method of Java Thread class.
  5. */
  6.  
  7. public class PauseThreadUsingSleep {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. /*
  12. * To pause execution of a thread, use
  13. * void sleep(int milliseconds) method of Thread class.
  14. *
  15. * This is a static method and causes the suspension of the thread
  16. * for specified period of time.
  17. *
  18. * Please note that, this method may throw InterruptedException.
  19. */
  20.  
  21. System.out.println("Print number after pausing for 1000 milliseconds");
  22. try{
  23.  
  24. for(int i=0; i< 5; i++){
  25.  
  26. System.out.println(i);
  27.  
  28. /*
  29. * This thread will pause for 1000 milliseconds after
  30. * printing each number.
  31. */
  32. Thread.sleep(1000);
  33. }
  34. }
  35. catch(InterruptedException ie){
  36. System.out.println("Thread interrupted !" + ie);
  37. }
  38.  
  39. }
  40. }
  41.  
  42. /*
  43. Output of this example would be
  44.  
  45. Print number after pausing for 1000 milliseconds
  46. 0
  47. 1
  48. 2
  49. 3
  50. 4
  51. */

Get Current Thread Example


  1. /*
  2. Get Current Thread Example
  3. This Java example shows how to get reference of current thread using
  4. currentThread method of Java Thread class.
  5. */
  6.  
  7. public class GetCurrentThreadExample {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. /*
  12. * To get the reference of currently running thread, use
  13. * Thread currentThread() method of Thread class.
  14. *
  15. * This is a static method.
  16. */
  17.  
  18. Thread currentThread = Thread.currentThread();
  19. System.out.println(currentThread);
  20.  
  21. }
  22. }
  23.  
  24. /*
  25. Output of the example would be
  26. Thread[main,5,main]
  27. */

Create New Thread Using Runnable Example


  1. /*
  2. Create New Thread Using Runnable Example
  3. This Java example shows how to create a new thread by implementing
  4. Java Runnable interface.
  5. */
  6.  
  7. /*
  8.  * To create a thread using Runnable, a class must implement
  9.  * Java Runnable interface.
  10.  */
  11. public class CreateThreadRunnableExample implements Runnable{
  12.  
  13. /*
  14. * A class must implement run method to implement Runnable
  15. * interface. Signature of the run method is,
  16. *
  17. * public void run()
  18. *
  19. * Code written inside run method will constite a new thread.
  20. * New thread will end when run method returns.
  21. */
  22. public void run(){
  23.  
  24. for(int i=0; i < 5; i++){
  25. System.out.println("Child Thread : " + i);
  26.  
  27. try{
  28. Thread.sleep(50);
  29. }
  30. catch(InterruptedException ie){
  31. System.out.println("Child thread interrupted! " + ie);
  32. }
  33. }
  34.  
  35. System.out.println("Child thread finished!");
  36. }
  37.  
  38. public static void main(String[] args) {
  39.  
  40. /*
  41. * To create new thread, use
  42. * Thread(Runnable thread, String threadName)
  43. * constructor.
  44. *
  45. */
  46.  
  47. Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");
  48.  
  49. /*
  50. * To start a particular thread, use
  51. * void start() method of Thread class.
  52. *
  53. * Please note that, after creation of a thread it will not start
  54. * running until we call start method.
  55. */
  56.  
  57. t.start();
  58.  
  59. for(int i=0; i < 5; i++){
  60.  
  61. System.out.println("Main thread : " + i);
  62.  
  63. try{
  64. Thread.sleep(100);
  65. }
  66. catch(InterruptedException ie){
  67. System.out.println("Child thread interrupted! " + ie);
  68. }
  69. }
  70. System.out.println("Main thread finished!");
  71. }
  72. }
  73.  
  74. /*
  75. Typical output of this thread example would be
  76.  
  77. Main thread : 0
  78. Child Thread : 0
  79. Child Thread : 1
  80. Main thread : 1
  81. Main thread : 2
  82. Child Thread : 2
  83. Child Thread : 3
  84. Main thread : 3
  85. Main thread : 4
  86. Child Thread : 4
  87. Child thread finished!
  88. Main thread finished!
  89.  
  90. */

Java Class example.


  1. /*
  2. Java Class example.
  3. This Java class example describes how class is defined and being used
  4. in Java language.
  5.  
  6. Syntax of defining java class is,
  7. <modifier> class <class-name>{
  8.   // members and methods
  9. }
  10. */
  11.  
  12. public class JavaClassExample{
  13. /*
  14.   Syntax of defining memebers of the java class is,
  15.   <modifier> type <name>;
  16.   */
  17. private String name;
  18. /*
  19.   Syntax of defining methods of the java class is,
  20.   <modifier> <return-type> methodName(<optional-parameter-list>) <exception-list>{
  21.   ...
  22.   }
  23.   */
  24. public void setName(String n){
  25. //set passed parameter as name
  26. name = n;
  27. }
  28. public String getName(){
  29. //return the set name
  30. return name;
  31. }
  32. //main method will be called first when program is executed
  33. public static void main(String args[]){
  34. /*
  35.   Syntax of java object creation is,
  36.   <class-name> object-name = new <class-constructor>;
  37.   */
  38. JavaClassExample javaClassExample = new JavaClassExample();
  39. //set name member of this object
  40. javaClassExample.setName("Visitor");
  41. // print the name
  42. System.out.println("Hello " + javaClassExample.getName());
  43. }
  44. }
  45.  
  46. /*
  47. OUTPUT of the above given Java Class Example would be :
  48. Hello Visitor
  49. */

How to find the most appropriate Keywords?

  🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...