adolfm
NFT Marketplace Expert
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.
3. Inside of it, display the output out from the For Loop using System.out.println.
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.
Full Source Code:
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
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.
- for
(
int
i =
1
;
i <=
5
;
i++
)
3. Inside of it, display the output out from the For Loop using System.out.println.
- 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.
- try
- {
- Thread
.sleep
(
1000
)
;
- }
catch
(
InterruptedException
ie)
- {
- ie.printStackTrace
(
)
;
- }
Full Source Code:
- public
class
addDelay
- {
- public
static
void
main(
String
[
]
args)
- {
- for
(
int
i =
1
;
i <=
5
;
i++
)
- {
- System
.out
.println
(
"i = "
+
i)
;
- try
- {
- Thread
.sleep
(
1000
)
;
- }
catch
(
InterruptedException
ie)
- {
- ie.printStackTrace
(
)
;
- }
- }
- }
- }
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.