Programming

Java Code to Wish Happy Birthday

If you’re a Java programmer and you want to wish your family member or friend on their birthday then as a programmer you may like to wish differently. 

So, in today’s article, I’ll tell you, how you can wish you anyone using a simple Java code. If you already know Java or only know the basics of Java programming, then you’re good to go and you’ll see what I’m doing in this code. 

First, I’ll write the code then I’ll show you the result. After that, I will tell you how the code works. So, let’s get started. 

Happy Birthday code in Java

import java.util.Scanner; 

public class main {
    public static void main(String[] args) {
        int delayMillis = 300;
        int numberOfFrames = 10; 
        int frameWidth = 50;

        Scanner obj = new Scanner(System.in);
        System.out.print("Enter your name: ");

        String userName = obj.nextLine();
        System.out.println("");
        for (int i = 0; i <= numberOfFrames; i++) {
            System.out.print("Loading [");
            int progress = (i * frameWidth) / numberOfFrames;
            for (int j = 0; j < frameWidth; j++) {
                if (j < progress) {
                    System.out.print("=");
                } else if (j == progress) {
                    System.out.print(">");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.print("] " + (i * 10) + "%");
            try {
                Thread.sleep(delayMillis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print("\r"); 
        }
        System.out.println("\n****************************************************\n");
        System.out.println("       Happy Birthday, " + userName +"!!!         ");
        System.out.println("\n****************************************************");
    }
}

Explaining the Code

I’m not going to explain line by line. I hope you already know the basics of Java programming. I’m just going to explain the process that what I did to complete this job. 

 

First I created some variables to store the value of loading animation speed, frame, and frame rate. After that, I input the user name from the user and I store that in a variable called userName. Afterward, I wrote the loading animation using a nested loop. 

Read More: 15 most important HTML interview questions and answers

After the loading animation part, I just print out the beautiful string written at the end of the code. In that string, I use the value of the userName value to wish that the specific user who is using the code entered his or her name at the first step after the program ran. 

Result of the Code

java

The thing that you will learn from this code

This wishing Java code is very easy. Although there are also a lot of things to learn. If you wrote this code by understanding each and every line then you’ll find a lot of stuff in this code. Let me tell you what things you are going to find in this code. 

  • Declaring Variables: In this code, we had to declare variables to store some value. So you’ll learn how to create variables in Java. 
  • Take Input: In this Java code to wish happy birthday we also took input of the user name. So, by doing this you are also going to know how to take input in Java. 
  • Nested Loop: Yes, you will know how to implement a loop in this code. But you are also going to know how to use nested loops. If you don’t know what is a nested loop then it’s a loop inside a loop. And when there is a loop inside another loop it’s called a nested loop. 
  • Print: At the end, we print out some strings and that’s how you also going to know how to print out something in Java. 

Conclusion

So, this is the Java code to wish happy birthday. By using this you can wish anyone. The code is written simply. I don’t want anything competitive and the reason for this is that, if the person that you’re going to wish asked you to teach him how this code works then that will be competitive also to explain him or her. 

That’s why I wrote a very simple code which will just take the birthday boy or birthday girl’s name and after that, this code will wish him or her. So, that’s all for today. If you have anything to ask you can tell me that in the comment section and happy coding!

What is your reaction?

0
Excited
1
Happy
0
In Love
0
Not Sure
0
Silly

Leave a reply

Your email address will not be published. Required fields are marked *