Java...If else statement

Java...If else statement

No alt text provided for this image
Java if-else example

Greetings! Today's article we will dive into Java statements, in particular, the if-else statement. We will also introduce the Scanner class (for user input) as well! Are you ready!?!??!?! Let's have some Java!

  1. Here we have the Scanner class. This class allows us to have user input so users can interact with our code. You will notice the new Scanner object with a variable name of 'scanner' (how convenient lol). Also, we need in parenthesis(System.in) to allow for input from an outside source(think keyboard).
  2. Next, we are simply printing out our question posed to the user.
  3. On line 3, we create a String 'day' to take in the user's input (scanner.NextLine()).
  4. Now, on line 4, the fun starts!!! Our if statement starts off with variable 'day' followed by the method 'equalsIgnoreCase'. This method allows the user to enter in small or LARGE caps (lol). We then have our day 'Monday', where if the user types it, the code will print the result.
  5. On line 5, we have an else-if statement that allows for another choice for the user to type in. It also has its own output.
  6. On line 6, we 'end' our if-else statement chain with the last printout should the user choose any other day besides Monday or Saturday.
  7. Next, we have to always 'close' our scanner so it knows that our user input has come to an end.
  8. In green, we can see the user's answer after the question prompt.
  9. Finally, we see the result of the user's selection. Whew!!! LOL!!!

We did it! We went through Java's if-else statement and it's Scanner class!! Now, we definitely need some Java to drink up after this exercise. I hope you learned something new today, thank you for taking the time to go through this article, and as always, stay Java!!!

Broadus Palmer

Cloud Engineering Is Your Next Career.

1 年

Nice I will check it out!!

要查看或添加评论,请登录

David K.的更多文章

  • What a year(2023 Recap)!!

    What a year(2023 Recap)!!

    Greetings friends! I know, I know, it has been some time since we've heard from one another, but let me explain a bit…

  • Java Arrays

    Java Arrays

    Greetings friends! Today we will begin our introduction to some infamous or famous, depending on who you ask lol, data…

  • Java - For and ForEach Loops

    Java - For and ForEach Loops

    Good day everyone! Today's topic, while we are sadly coming to the end of the conditional statements, will revolve…

    4 条评论
  • Java - While statement

    Java - While statement

    Greetings Java fam!!! In this edition of the New Dev Journey, we will touch on the Java while statement. The great…

  • Java's Do-While statement

    Java's Do-While statement

    Good morning everyone! Today's topic will be another one of Java's conditional statements, the do-while statement. The…

    4 条评论
  • Java Primitive Data Types???

    Java Primitive Data Types???

    Hello everyone! Today's example will be Java's eight(8) primitive data types. These data types are very important to…

  • Java Class Example

    Java Class Example

    So, today, we are continuing our journey of learning Java. I wanted to show a small example of a Java class, attributes…

    2 条评论
  • What does a Java Program look like???

    What does a Java Program look like???

    Greetings everyone! I have posted a small snippet of what a Java program looks like. If you have read any of the…

  • Java - Objective-Oriented Programming principles

    Java - Objective-Oriented Programming principles

    So, the last time we left off, we were introduced to writing our first piece of Java code and what that looks like…

  • Java - where do I start?

    Java - where do I start?

    So, last week was a brief introduction into Java and I do mean brief. Now, I want to dive into and answer the question,…

社区洞察

其他会员也浏览了