• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to add a delay in your program in Java

adolfm

NFT Marketplace Expert
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
168
Likes
57
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
For this Java tutorial today, i will going to teach you how to create or a delay in your program.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of addDelay.java.

We will not import anything here because we will not access any class out from the packages.

2. In your Main, create a For Loop statement that has a variable i in there which starts at 1, end at 5, and has to be incremented by 1.

  1. for

    (

    int

    i =

    1

    ;

    i <=

    5

    ;

    i++

    )

3. Inside of it, display the output out from the For Loop using System.out.println.

  1. System

    .out

    .println

    (

    "i = "

    +

    i)

    ;

4. Now, create a try and catch method for trapping errors.

Here in your try method, we will add a delay in our program by one second before continue to print the next value of the loop using the Thread.sleep() method. If we want to have a one second delay then put 1000 as the parameter of the Thread.sleep() method because it has a millisecond time. In your Catch method, prefer to catch InterruptedException then use printStackTrace() method. This will help to trace the exception and identify which method causes the bug.

  1. try
  2. {
  3. Thread

    .sleep

    (

    1000

    )

    ;

  4. }

    catch

    (

    InterruptedException

    ie)
  5. {
  6. ie.printStackTrace

    (

    )

    ;
  7. }

Full Source Code:
  1. public

    class

    addDelay
  2. {
  3. public

    static

    void

    main(

    String

    [

    ]

    args)
  4. {

  5. for

    (

    int

    i =

    1

    ;

    i <=

    5

    ;

    i++

    )
  6. {

  7. System

    .out

    .println

    (

    "i = "

    +

    i)

    ;

  8. try
  9. {
  10. Thread

    .sleep

    (

    1000

    )

    ;

  11. }

    catch

    (

    InterruptedException

    ie)
  12. {
  13. ie.printStackTrace

    (

    )

    ;
  14. }
  15. }
  16. }
  17. }

Press F5 to run this.

Download the source code and try it! :)

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,672

349,682

Top