READING USER INPUT

package com.cnc;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter your year of birth : ");

boolean hasNextInt = scanner.hasNext();

if(hasNextInt) {
int yearOfBirth = scanner.nextInt();
scanner.nextLine(); // when you not press the enter key the
// machine thinks the enter key as
// the input for the name and
// missed the chance .So to overcome get a
// chance scanner.nextLine(); is used


System.out.println("Enter your Name: ");
String name = scanner.nextLine();
int age = 2020 - yearOfBirth;

if (age >= 0 && age <= 100) {
System.out.println("Your nam is " + name + ", you are " + age + " year old.");
} else {
System.out.println("Invalid Year of birth");
}

}else System.out.println("Unable to parse year of birth");

scanner.close();
}
}

Popular posts from this blog

36 LOGICAL "OR" OPERATOR

Arrays

ENCAPSULATION