4. Java – Simple IF-ELSE

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorld {
public static void main(String[] args) throws IOException
  System.out.println("Please Enter your Age : ");
  // Enter data using BufferReader
  BufferedReader reader = new BufferedReader(
  new InputStreamReader(System.in));
   // Reading data using readLine     
 String StringAge = reader.readLine();     
 // readLine can return only string and hence     
 //we have to convert String to Integer.     
 int age = Integer.parseInt(StringAge);     
 if (age > 18) 
 //simple if condition     
  System.out.println("Eligible for Driver's License");     
  System.out.println("Not Eligible for Driver's License");

Please note – readLine( ) method returns all input as string but if-else condition needs a proper integer for age. So we have converted string to int using method Integer.ParseInt( )


%d bloggers like this: