JAVA-Ipnut and Output - Notes By ShariqSP
Input and Output Statements in Java
In Java, input and output operations are fundamental for interacting with the user and displaying information. The primary classes for handling input and output are Scanner
and System.out.println()
respectively.
Input Statements
Input statements are used to gather data from the user or from an external source.
- Scanner class: This class provides methods to read input from various sources like the keyboard, files, etc. It's commonly used for taking input from the user.
Example:
// Example of taking integer input
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
System.out.println("You entered: " + num);
}
}
Output Statements
Output statements are used to display data to the user or to write data to an external destination.
- System.out.println() method: This method is used to print data on the console. It prints the argument passed to it followed by a newline character.
Example:
// Example of printing a double value
public class OutputExample {
public static void main(String[] args) {
double num = 3.14;
System.out.println("Value of pi: " + num);
}
}
Input and Output Examples for Primitive Data Types
Below are examples demonstrating how to take input and print output for various primitive data types:
1. Integer:
// Taking input and printing output for integer
import java.util.Scanner;
public class IntegerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
System.out.println("You entered: " + num);
}
}
2. Double:
// Taking input and printing output for double
import java.util.Scanner;
public class DoubleExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a double value: ");
double num = scanner.nextDouble();
System.out.println("You entered: " + num);
}
}
3. Float:
// Taking input and printing output for float
import java.util.Scanner;
public class FloatExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a float value: ");
float num = scanner.nextFloat();
System.out.println("You entered: " + num);
}
}
4. Char:
// Taking input and printing output for char
import java.util.Scanner;
public class CharExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
System.out.println("You entered: " + ch);
}
}
5. Boolean:
// Taking input and printing output for boolean
import java.util.Scanner;
public class BooleanExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a boolean value (true/false): ");
boolean bool = scanner.nextBoolean();
System.out.println("You entered: " + bool);
}
}
6. Byte:
// Taking input and printing output for byte
import java.util.Scanner;
public class ByteExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a byte value: ");
byte num = scanner.nextByte();
System.out.println("You entered: " + num);
}
}
7. Short:
// Taking input and printing output for short
import java.util.Scanner;
public class ShortExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a short value: ");
short num = scanner.nextShort();
System.out.println("You entered: " + num);
}
}
8. Long:
// Taking input and printing output for long
import java.util.Scanner;
public class LongExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a long value: ");
long num = scanner.nextLong();
System.out.println("You entered: " + num);
}
}
9. String:
// Taking input and printing output for String
import java.util.Scanner;
public class StringExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
System.out.println("You entered: " + input);
}
}
Differences Between print(), println(), and printf() Methods in Java
In Java, the print()
, println()
, and printf()
methods are used for printing output to the console. While they all serve similar purposes,
they have distinct differences in their behavior and usage.
Method | Description | Usage |
---|---|---|
print() |
Prints the given argument to the console without advancing to the next line. | System.out.print("Hello"); |
println() |
Prints the given argument to the console and moves to the next line. | System.out.println("Hello"); |
printf() |
Prints formatted output to the console based on a format string and arguments. | System.out.printf("Hello %s", name); |
The print()
method is useful when you want to print multiple items on the same line. The println()
method automatically moves to the next line after printing. The printf()
method allows for formatted output with placeholders for variables.
Differences Between next() and nextLine() Methods in Java
In Java, the next()
and nextLine()
methods are used for reading input from the user via the keyboard. These methods have differences in how they handle whitespace and newline characters.
Method | Description | Usage |
---|---|---|
next() |
Reads the next token (sequence of characters separated by whitespace) from the input stream. | String word = scanner.next(); |
nextLine() |
Reads the next line of input including any leading and trailing whitespace characters. | String line = scanner.nextLine(); |
The next()
method reads only the next token until whitespace or newline, while the nextLine()
method reads the entire line including whitespace and newline characters. It's important to choose the appropriate method based on the desired input behavior.