User Input with Scanner
The Scanner class from java.util package enables reading user input. Here's the basic syntax:
InputReader reader = new InputReader(System.in);
Use next() and nextLine() methods to read strings, with hasNext() and hasNextLine() to check for available input.
next() Method
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter text: ");
String text = input.next();
System.out.println("You entered: " + text);
input.close();
}
}
Output when entering "hello world":
Enter text: hello world
You entered: hello
Key characteristics of next():
- Requires valid input characters before completion
- Ignores leading whitespace
- Treats trailing whitespace as delimiters
- Cannot read strings containing spaces
Reading Different Data Types
import java.util.Scanner;
public class MultiTypeInput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter integer: ");
if(input.hasNextInt()) {
int number = input.nextInt();
System.out.println("Integer: " + number);
}
System.out.print("Enter decimal: ");
if(input.hasNextFloat()) {
float decimal = input.nextFloat();
System.out.println("Decimal: " + decimal);
}
input.close();
}
}
nextLine() Method
import java.util.Scanner;
public class LineInput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter text: ");
String text = input.nextLine();
System.out.println("Full line: " + text);
input.close();
}
}
Output when entering " hello world":
Enter text: hello world
Full line: hello world
Key characteristics of nextLine():
- Reads until Enter key is pressed
- Returns all character including spaces
- Preserves leading and trailing whitespace
Sequential Execution
Java executes code sequentially by default, processing statements in the order they appear unles directed otherwise by control structures.
Conditional Statements
Single if Statement
if(condition) {
// Executes when condition is true
}
if-else Statement
if(condition) {
// True branch
} else {
// False branch
}
Multiple Conditions
if(condition1) {
// First true case
} else if(condition2) {
// Second true case
} else if(condition3) {
// Third true case
} else {
// All false case
}
Nested if Statements
if(outerCondition) {
if(innerCondition) {
// Both conditions true
}
}
Switch Statement
switch(variable) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Default case
}
Switch supports byte, short, int, char, and String types. Each case must be a constant value.
Loop Structures
While Loop
while(condition) {
// Loop body
}
Do-While Loop
do {
// Loop body
} while(condition);
For Loop
for(initialization; condition; update) {
// Loop body
}
Enhanced For Loop
for(dataType element : collection) {
// Process element
}
public class ArrayIteration {
public static void main(String[] args) {
int[] values = {10, 20, 30, 40};
for(int value : values) {
System.out.println(value);
}
}
}
Loop Control: Break and Continue
Break Statement
public class BreakExample {
public static void main(String[] args) {
int counter = 0;
while(counter < 100) {
counter++;
System.out.println(counter);
if(counter == 30) {
break;
}
}
}
}
Continue Statement
public class ContinueExample {
public static void main(String[] args) {
int number = 0;
while(number < 10) {
number++;
if(number % 2 == 0) {
continue;
}
System.out.println(number);
}
}
}
Labeled Statements
public class PrimeNumbers {
public static void main(String[] args) {
outer: for(int num = 101; num < 150; num++) {
for(int divisor = 2; divisor < num/2; divisor++) {
if(num % divisor == 0) {
continue outer;
}
}
System.out.println(num);
}
}
}