Taking Input and Conditional Statements in Java

I am an engineering student pursuing a degree in Artificial Intelligence and Data Science at Datta Meghe College of Engineering. I have strong technical skills in Full Stack Web Development, as well as programming in Python and Java. I currently manage Doubtly's blog and am exploring job opportunities as an SDE. I am passionate about learning new technologies and contributing to the tech community.
Interacting with users and making decisions based on input are fundamental programming skills. In this guide, we’ll cover how to take input in Java and use conditional statements to make decisions. Let’s dive in!
1. Taking User Input in Java Using Scanner
Java provides the Scanner class to take user input from the console. This class is part of the java.util package.
Steps to Take Input
Import the
Scannerclass:import java.util.Scanner;Create a
Scannerobject:Scanner sc = new Scanner(System.in);Use methods like
nextInt(),nextLine(),nextDouble(), etc., to read user input.
Example: Taking Input from the User
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
sc.close();
}
}
2. Java Conditional Statements: An Overview
Conditional statements control the flow of your program by making decisions based on conditions. The main conditional statements in Java are:
If Statement
If-Else Statement
Nested If-Else Statement
Ternary Operator
Switch Statement
3. If-Else in Java: Making Decisions
The if-else statement allows your program to execute a block of code if a condition is true and another block if it’s false.
Syntax
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Example: Checking Even or Odd
import java.util.Scanner;
public class IfElseExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
sc.close();
}
}
4. Nested If-Else Statements for Complex Conditions
When you have multiple conditions to check, you can use nested if-else statements.
Example: Grading System
import java.util.Scanner;
public class NestedIfElseExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your marks: ");
int marks = sc.nextInt();
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 75) {
System.out.println("Grade: B");
} else if (marks >= 50) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
sc.close();
}
}
5. Using the Ternary Operator in Java
The ternary operator is a shorthand for simple if-else conditions. It has the syntax:
variable = (condition) ? value_if_true : value_if_false;
Example: Finding the Larger of Two Numbers
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("The larger number is: " + max);
}
}
6. Java Switch Statements: An Alternative to If-Else
The switch statement is a cleaner alternative when you have multiple conditions based on a single variable.
Syntax
switch (variable) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// default code block
}
Example: Day of the Week
import java.util.Scanner;
public class SwitchExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (1-7): ");
int day = sc.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid input");
}
sc.close();
}
}
Conclusion
Taking user input and making decisions with conditional statements are essential skills in Java. From simple if-else to nested conditions, ternary operators, and switch statements, each tool serves a specific purpose. With these examples, you’re now equipped to handle a wide range of scenarios in your Java programs.
Happy coding!




