Lesson 5 Input in Java
Lesson 5 Input in Java
Computer Applications
Input in Java
Topics to be covered
1. Introduction to Packages
2. Initialization
3. Scanner Class
4. Functions of Scanner class
5. Types of errors
6. Types of comments
Package
• A package is a collection of classes. Every Java package has a
name and it can be included within the program by using the
keyword import. These are used to perform various tasks.
There are different packages->
a. java.util -> To perform utility task for transferring data
between and within application
b. java.io-> To perform input output tasks
c. java.lang-> To perform calculations on String and
mathematical variables
How to input the data in a variable?
1. Initialization (using Assignment Operator)-> It is the type
of input we give when the value is assigned to a variable
using an assignment operator before the time of
compilation. class demo1
{
public static void main()
{
int a=10, b=20 ,c;
a =a+10;
b=b+14;
c=a+b;
System.out.println(a+" "+b+" "+c);
}
}
How to input the data in a variable?
2. By using Function Argument(Parameterized Input)-> It is
the type of input we give as the parameters in the main
function at the time of compilation.
class demo2
{
public static void main(int a, int b)
{
int c;
a =a+10;
b=b+14;
c=a+b;
System.out.println(a+" "+b+" "+c);
}
}
How to input the data in a variable?
3. By using Stream classes-> There are two classes i.e.
‘InputStream’ and ‘BufferedReader’.
a. InputStream -> The data given to the computer is
converted into machine code. The machine code needs
to be transferred to the CPU in terms of a binary bit
pattern. The streamlining of bits is done by using
InputStream.
b. BufferedReader -> A computer has a high speed buffer
device in between input unit and processor. In order to
synchronise the speeds of I/O device and processor,
buffer allows storing of input streamlined bit pattern so
that instant supply of data can be done to the CPU
without any delay.
How to input the data in a variable?
import java.io.*;
public class demo3
{
public static void main() throws IOException
{
int a;
InputStreamReader i1 = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(i1);
System.out.println("Enter a number");
a = Integer.parseInt(br.readLine());
System.out.println("The value of a =" +a);
}
}
How to input the data in a variable?
4. By using Scanner classes-> We use Scanner class to input
the data in a variable at the time of execution. It is used to read
the values of various types. It is defined within a Java package
(java.util.*) . We need to create an object of Scanner class to input
the data of various types. The syntax is :
Scanner objectname = new Scanner(System.in);
For Example: Scanner sc = new Scanner(System.in);
Methods of Scanner Class
Function Name Purpose Syntax
nextByte() To input byte type of data. byte a = sc.nextByte();
nextShort() To input short type of data. short a = sc.nextShort();
nextInt() To input integer type of data. int a = sc.nextInt();
nextFloat() To input float type of data. float a = sc.nextFloat();
nextLong() To input long type of data. long a = sc.nextLong();
nextDouble() To input double type of data. double a = sc.nextDouble();
next() To input a single word String s = sc.next();
nextLine() To input the whole sentence String s = sc.nextLine();
next().charAt(0) To input a single character char ch = sc.next().charAt(0);
nextBoolean() To input a boolean type data. boolean b = sc.nextBoolean();
Difference between Testing and Debugging
Testing Debugging
Performed by developer or
Performed by testers
development team.
It is a process in which the errors in
It is a process in which a program
the program are removed.
is validated.