OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Part-5: User Input to Programs, Escape Sequences

 

User Input to Programs

Here, we make use of the class Scanner of package java.util to give input to the program. Basically, Java Scanner class is a text scanner that breaks the input into using a delimiter. In Java program, importing the class Scanner from package is the first step and should be declare object for performing above stated action.

 

Example: Addition of two integer values

import java.util.Scanner;

// importing Scanner class

public class Add

{

public static void main(String args[])

{

Scanner sc = new Scanner(System.in);   // Declaring object

int a, b;

System.out.println(“Enter any two integer values: ”);

  a = sc.nextInt();      

 b = sc.nextInt();// Here, sc invokes the method nextInt() which reads the value typed by the user.

         System.out.println(“The Sum of given two integer values is : ”+(a+b));

}

}


Java Program: Input for Different Data Types

import java.util.Scanner;
public class UserInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Integer input System.out.print("Enter an integer: "); int intValue = scanner.nextInt(); // Float input System.out.print("Enter a float value: "); float floatValue = scanner.nextFloat(); // Double input System.out.print("Enter a double value: "); double doubleValue = scanner.nextDouble(); // Character input System.out.print("Enter a character: "); char charValue = scanner.next().charAt(0); // String input (single word) System.out.print("Enter a single word: "); String singleWord = scanner.next(); // String input (whole line) scanner.nextLine(); // Consume newline left-over System.out.print("Enter a full sentence: "); String fullLine = scanner.nextLine(); // Boolean input System.out.print("Enter true or false: "); boolean boolValue = scanner.nextBoolean(); // Displaying the inputs System.out.println("\n===== Output ====="); System.out.println("Integer: " + intValue); System.out.println("Float: " + floatValue); System.out.println("Double: " + doubleValue); System.out.println("Character: " + charValue); System.out.println("Single Word: " + singleWord); System.out.println("Full Sentence: " + fullLine); System.out.println("Boolean: " + boolValue); scanner.close(); } }

💻 Sample Output:

Enter an integer: 10
Enter a float value: 12.34 Enter a double value: 12345.6789 Enter a character: A Enter a single word: Hello Enter a full sentence: This is Java input example. Enter true or false: true ===== Output ===== Integer: 10 Float: 12.34 Double: 12345.6789 Character: A Single Word: Hello Full Sentence: This is Java input example. Boolean: true

===============================================================================================

Escape Sequences




These characters are consists of a backslash followed by a letter and have special meaning for the compiler. The character escape sequences are as follows:



In fact, these are the instructions to for manipulations in formatting and character representation. For example, “\u0041” is the representation of letter A.

Comments

Popular posts from this blog

Career Guide - B.Tech Students

Artificial Intelligence - UNIT - 1 Topic - 1 : Introduction to AI (Artificial Intelligence)

Financial Aid for Students: Scholarships from Government, NGOs & Companies