OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : 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));
}
}
Comments
Post a Comment