0% found this document useful (0 votes)
4 views

Lesson 5 Input in Java

The document provides an overview of input methods in Java, including the use of packages, initialization, Scanner class, and error types. It explains how to input data using various techniques such as assignment, function arguments, stream classes, and the Scanner class, along with methods of the Scanner class. Additionally, it discusses the differences between testing and debugging, types of errors, and comment statements in Java programming.

Uploaded by

k.anany1750
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lesson 5 Input in Java

The document provides an overview of input methods in Java, including the use of packages, initialization, Scanner class, and error types. It explains how to input data using various techniques such as assignment, function arguments, stream classes, and the Scanner class, along with methods of the Scanner class. Additionally, it discusses the differences between testing and debugging, types of errors, and comment statements in Java programming.

Uploaded by

k.anany1750
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Std IX

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.

It is complete when all the desired It is finished when there is no error


verifications against specifications and the program is ready for
have been performed. execution.
Types of Errors
Errors are the mistakes in a program or sometimes the user doesn’t
get the desired output. There are three types of Errors:-
a. Syntax Error - These errors occur when the rules of the
programming language are not followed. The program is not able to
give the output if the syntax error is there.
b. Runtime Error – Sometimes, the program is not producing the
desired output even after the errors are removed and program is
compiled. For example: dividing a number by zero, trying out to find
the square root of a negative number.
c. Logical Error – It is the error in the programming logic. For Example:
applying the wrong formula or giving the wrong input.
Identify the types of errors
a. System.out.println( "Hello World")
b. class Test
{
public static void main()
{ int a=0, b=5,c; c=b/a;
System.out.println( "RESULT"+c); }
}
c. class Test
{
public static void main() {
double sal = 5000;
sal =sal*10/100;
System.out.println( sal );
} }
Comment Statements in Java Programming
• Comments are used to add remarks in a program. These are
non-executable statements that enables the readers/users to
understand the program logic.
• Types of comments:-
1. Single Line Comment (//)
2. Multi Line comment (/* ………………*/)
3. Documentation Comment (/** …………*/)
QUESTIONS ANSWERS (Kindly write the answers in your
Computer Applications notebook.)
1. Explain the difference between declaration and initialization of a
variable.
2. What is a package? Name any two packages.
3. Mention two different ways of expressing a comment in a program.
4. What is the difference between the Scanner class functions next() and
nextLine()?
5. Name the type of error in each of the following cases:
(i) Division by a variable that contains a value zero
(ii) Multiplication operator is used when the operation should be
division.
(iii) Missing semicolon
YO U!
T H A N K

You might also like