You are on page 1of 11

Lecture No 2

How to take input from user in C++ JAVA

Input In Java
Input in java occurred through Scanner class
Scanner is the built in class of java.
Scanner class is present in java.util package

so we import this package in our program
We can access this class by creating object
of this class
Always use S capital because java is case

Input in java Continued
For taking input from user method will be
Scanner o=new Scanner(;
In the above sentence we use Scanner class

by creating object o
To use Scanner Class we must create an
object that point to this Scanner class
New keyword is used to allocate the
 is input stream used to get input
from user

Scanner a=new Scanner(;
Int number=a.nextInt();
Here a is the object name nextInt is used to

input an integer and stored it in a variable
named as number
Float number=a.nextFloat();
Here nextFloat is used to input float number
and stored it in variable named as float
Double number=a.nextDouble();

Char Input
For character input we use following method
Scanner a =new Scanner(;
We get the string by this method and takes it

only first character by using charAt(0).
As it takes only first character of string.

Input In C++
For taking input from user in c++
We use cout and cin method to display and

take input from the user
For using cout and cin we must use header file
known as #include<iostream> if we don’t
include this header file our programme will not
compile and it gives error that cout was not
declared in this scope it takes cout as variable
not function of print.Same case with cin

Input In C++ Continued
After Including #include<iostream>
We use using using namespace std;
Using namespace std; is instructing our C++

compiler to use the standard C++ library.  If
you don't give this instruction, then you will
have to use std::, each time you use a
standard C++ function/entity.
If you don’t use Using namespace std our
programme will not compile and throws error
on cout and cin

Input In C++ Continued
For cin we use >>
For cout we use <<
Int a;
Cout<<“entre number”;
Cout will display the string that is entre

Cin will take the integer value and store it in
the variable named as a.

Comparison of inputs

In C++

 In java we use Scanner

class which is built in class
of java
 To access this class we
create an object of this
 nextInt() will only input
integer values.
 We must import

 In c++ we use cout and

cin built in methods
 For using cout and cin we
must include<iostream>
and Using namespace std;
 For cout use <<
 For cin use >>

Simple Example
In C++
 #include<iostream>
 using namespace std;
 int main()

int a,b;

cout<<"entre number 1"<<endl;


cout<<"entre number 2"<<endl;


int add;


cout<<"after addition ans is "<<add;
 Thanks to Devc++

In Java
 */
 public class JavaApplication15 {
 /**

* @param args the command line arguments

 public static void main(String[] args) {

int a,b;

System.out.println("entre number 1");

Scanner c=new Scanner(;


System.out.println("entre number 2");


int add;


System.out.println("After addition "+add);
 }

 Thanks to netbeans

Thanks alot
Contact me at
Mobile no:0336-5686312