Computer Application IX InputInJava
Computer Application IX InputInJava
Comments in Java: Comments are used to describe the code. It helps in understanding the
logic applied in the program. There are following ways to provide comments-
// Used for single line comment.
Package: A package is a named collection of related compiled classes. All library classes are
grouped under various Java packages. A package inside of another package is known as
sub-package. For example a library class Math is a part of lang sub-package of Java
package. We can refer it as Java.lang.Math . Following are some useful packages-
Package Description
java.lang Default package that provides basic classes to design a java program.
It contains primitive data types, String class, Math class etc.
java.util Utility classes such as Scanner, Date etc.
java.io Classes for input / output.
java.awt Abstract Windows Tools provides classes to implement GUI.
java.net Classes for networking.
java.applet Classes for implementing applets.
We can create own package using package keyword. To create package we write as-
package packageName ;
class ClassName
{
…
}
We can import classes from other packages in our program using import keyword. For
example- to import Scanner class, we write: import java.util.Scanner ;
Since java.lang is a default package of Java language so we don’t need to import it.
Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 18
Input using the Scanner class: Java Scanner class allows us to take input from the
console during run time. It belongs to java.util package.
It also converts the Bytes (from the input stream) into characters.
Input / Output Streams: A java program uses a stream either to read (input) data items
from a source or to write (output) data items to a destination. Here a stream is a path
along which the data flows.
Reads Writes
Source Program Destination
(Keyboard) Input Stream Output Stream (Monitor)
The Scanner breaks its input into tokens using a delimiter (space is default delimiter).
Method Description
nextBoolean() It is used to scan the next token of the input into a boolean value.
next().charAt(0) It is used to scan the next token of the input as a string and returns the first character only.
There are some methods to check the validity of tokens such as- hasNext(), hasNextInt(),
hasNextFloat() etc. These methods return boolean value (true / false) after checking the
Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 19
validity of tokens. For example – hasNextInt() returns false if next token is ten and
returns true if next token is 10.
Errors in Java program: Errors are mistakes that affect the functioning of a
program. Errors are often referred to as bugs. The process of finding and eliminating
errors is called debugging. Errors can broadly be classified into three categories-
Syntax errors: These are spelling or grammatical mistakes according to Java
language. These are also known as compile time errors. For example- Missing
semicolon, Missing braces, Undefined variables etc.
Logical errors: A logical error occurs when the program compiles and runs without
errors but produces an incorrect result. Some common reason of logical errors are-
incorrect formula, incorrect logic applied etc.
Run time errors: These errors occur during the execution of a program, due to
some illegal operations performed in the program. Examples of some illegal
operations are- Dividing a number by zero, Input type mismatch, insufficient
memory etc.
Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 20