Accepting User Input using Scanner class (Java Source Code)

NetBeans IDE : Accepting User Input using Scanner class (Java Tutorial with Source Code).

👨‍💻 Here is my online course: Visual Basic .NET (VB.NET), Access Database and Crystal Reports Course.
🎯 Enroll now (Full lifetime access): http://bit.ly/2YRy99d

Screenshot: Java EP.4 - Accepting User Input.



Java (Part 2/2): https://youtu.be/Q3KsX-lEKdE

[FREE SOURCE CODE by iBASSKUNG]

#BEGIN

package javauserinput;

import java.util.Scanner;
import java.util.Calendar;
import java.util.InputMismatchException;

/**
 *
 * @author iBasskung
 */

public class JavaUserInput {

    private static String appTitle = "\nHello, Welcome to User Input App (Scanner Class).\n";
    private static String fullName = "";
    private static int currentYear = 0;
    private static int yearYouBorn = 0;
    private static int yourAge = 0;
 
    // Method
    private static void printSummary(){
        System.out.println("Summary of user input:");
        // Formatting string.
        System.out.printf("- Name: %s.%n- Born in: %d.%n- Age: %d.%n%n", fullName, yearYouBorn, yourAge);
    }
 
    public static void main(String[] args) {
     
        System.out.println(appTitle); // Well done :)
     
        Scanner input = new Scanner(System.in);
     
     
        // infinite while loop
        while (true) {
         
            System.out.print("Press c to continue, press E to exit: ");
         
            String enteredByUser = input.nextLine();
         
            if(enteredByUser.equals("E")){
                // Done :)
                System.out.println("\nYou pressed: \'E\' : Upper case (Exit app). Have a good day!\n");
                return; // Exit function.
            }else if(enteredByUser.toLowerCase().equals("c")){
                System.out.println("\nYou pressed: \'c/C\' : Ignore case (Continue).\n");
             
                System.out.print("Please input your full name: ");
                fullName = input.nextLine();
                // print
                // Nothing was entered.
                if(fullName.isEmpty()){
                    System.out.println("Full name is empty. Please try again.\n");
                    continue;
                    // To skip the rest of the instructions in the while loop
                    // and begin the next iteration, use a continue statement.
                }
             
                System.out.printf("\nHello, %s!%n", fullName);
                System.out.println(""); // Empty line.
             
                System.out.print("What year were you born?: ");
             
                try{
                 
                    currentYear = Calendar.getInstance().get(Calendar.YEAR);
                    yearYouBorn = input.nextInt();
                    yourAge = currentYear - yearYouBorn;
                 
                    if(yearYouBorn > currentYear){
                        System.out.printf("\n(%d) greater than current year. Please try again.\n\n", yearYouBorn);
                        input.nextLine();
                        continue;
                    }else if(yourAge < 1 || yourAge > 100){ // 2020 1919 etc.
                        input.nextLine();
                        System.out.printf("\nYou were born in: %d, Your age is: %d!\n", yearYouBorn, yourAge);
                        System.out.println("\nInvalid Age! Age must be between 1 and 100.\n");
                        continue;
                    }else if(yourAge < 18){ // 2003 = 17 etc.
                        input.nextLine();
                        System.out.printf("\nYou were born in: %d, Your age is: %d!\n", yearYouBorn, yourAge);
                        System.out.println("\nSorry, you are underage for this application.\n");
                        continue;
                    }else{ // <= 2002 = 18, 2001 = 19, 2000, 1999, 1998 etc.
                     
                        System.out.printf("\nYou were born in: %d, Your age is: %d!\n", yearYouBorn, yourAge);
                        System.out.println("\n*** The mission has been comleted ***\n");
                     
                        printSummary(); // Method.
                     
                        break; // Exit while loop.
                     
                    }
                 
                }catch(InputMismatchException e){
                    System.out.println("\nAn error occurred: You did not input an integer.\n");
                    // clear
                    input.nextLine();
                }catch(Exception e){
                    System.out.println("\nAn error occurred: " + e.getMessage() + ".\n");
                    input.nextLine();
                }finally{
                    // The finally block always executes.
                    // System.out.println("The finally block executed.");
                }
             
            }
         
        }
     
        System.out.println("Thank you so much for watching my video.");
        System.out.println("Please like and subscribe to my channel.\n"); // \n = new line.
     
    }
 
}

#END

Follow me around
✔ Want to get updates on new courses or other cool free stuff? Just follow me on social media if that's your thing!

📺 Pages:
📍 https://www.facebook.com/CodeAMinute
📍 https://www.facebook.com/IbasskungTutorial
📍 https://www.facebook.com/codewithibasskung

📺 YouTube:
📍 https://www.youtube.com/c/iBasskung

📺 Udemy (Online Courses):
📍 https://www.udemy.com/vbnet-crystal-reports

📺 Twitter:
📍 https://twitter.com/#!/IBasskung

📺 Blogger:
📍 .NET: https://codeaminute.blogspot.com
📍 JAVA: https://javacodeminutes.blogspot.com

💯 THANK YOU SO MUCH 💯

#Java #JDK #NetBeans #AcceptingUserInput #ScannerClass #WhileLoop

Comments

Popular posts from this blog

How to Create an executable JAR file in NetBeans IDE (Java Source Code)

How to Create a Super Hello World Program in Java with Source Code