Video:37
SCANNER CLASS IN JAVA:
IN THIS CASE WWE WANT FULL NAME WE CAN USE
input.nextLine() keyword other wise we use
input.next() keyword it will print only alice
1)read int char double boolean float from the the user
import java.util.Scanner;
public class DataTypeInput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Reading an integer
System.out.print("Enter an integer: ");
int intValue = input.nextInt();
// Reading a character
System.out.print("Enter a character: ");
char charValue = input.next().charAt(0); // .charAt(0) extracts the first character from
the string input
// Reading a double
System.out.print("Enter a double: ");
double doubleValue = input.nextDouble();
// Reading a boolean
System.out.print("Enter a boolean (true/false): ");
boolean booleanValue = input.nextBoolean();
// Reading a float
System.out.print("Enter a float: ");
float floatValue = input.nextFloat();
// Display the values
System.out.println("\nYou entered the following values:");
System.out.println("Integer: " + intValue);
System.out.println("Character: " + charValue);
System.out.println("Double: " + doubleValue);
System.out.println("Boolean: " + booleanValue);
System.out.println("Float: " + floatValue);
input.close(); // Close the scanner
}
}
2)store the value inside the user in java
import java.util.Scanner;
public class UserInputStorage {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner input = new Scanner(System.in);
// Prompt and store different data types
System.out.print("Enter an integer: ");
int intValue = input.nextInt();
System.out.print("Enter a floating-point number: ");
float floatValue = input.nextFloat();
System.out.print("Enter a double: ");
double doubleValue = input.nextDouble();
System.out.print("Enter a character: ");
char charValue = input.next().charAt(0); // Reads first character of the input string
System.out.print("Enter a boolean (true/false): ");
boolean boolValue = input.nextBoolean();
// Display the stored values
System.out.println("\nStored values:");
System.out.println("Integer: " + intValue);
System.out.println("Float: " + floatValue);
System.out.println("Double: " + doubleValue);
System.out.println("Character: " + charValue);
System.out.println("Boolean: " + boolValue);
// Close the scanner
input.close();
}
}
3)perform some arthematic operator from the user
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
// Create Scanner object to read user input
Scanner input = new Scanner(System.in);
// Prompt user to enter two numbers
System.out.print("Enter the first number: ");
double num1 = input.nextDouble();
System.out.print("Enter the second number: ");
double num2 = input.nextDouble();
// Perform arithmetic operations
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double quotient = num1 / num2;
double modulus = num1 % num2;
// Display the results
System.out.println("\nResults of arithmetic operations:");
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Modulus: " + modulus);
// Close the scanner
input.close();
}
}
Video:40
Java exercise
JAVA PROGRAMING LITERALS:
ASSIGNMENT POERATURE :
OPERATORS : IN DETAILS
IIII
INCREAMENT OR DECREAMENT OPERATORS:
CASTING IN JAVA:
#SCONDITIONAL OPERATORS IN JAVA: