Data processing in Java
ICSE Class 9 · Computer Applications · Chapter 6
What this chapter covers
- Three ways a program receives its data
- Initialisation: data before execution
- Input through parameters: data at the time of execution
- Input stream: the Scanner class
- Scanner methods for whole numbers: nextShort(), nextInt() and nextLong()
- Scanner methods for real numbers: nextFloat() and nextDouble()
- Reading words, lines and characters: next(), nextLine() and next().charAt(0)
- Types of errors: syntax, runtime and logical
- Types of comments: single-line and multi-line
Key points
- Data processing means calculating, comparing and arranging input data to produce meaningful output.
- Data reaches a variable in three ways: initialisation (before execution), parameters (at the time of execution) and the Scanner class (during execution).
- Initialisation fixes the value in the source code; static initialisation uses a literal, dynamic initialisation uses an expression.
- Parameters are declared in the method header, such as `void main(int a, int b)`, and their values are entered when the method is called.
- A package is a group of related classes; `java.lang` is imported by default, while `java.util` must be imported for the Scanner class.
- A Scanner object is created with `Scanner sc = new Scanner(System.in);`, where `System.in` is the standard input stream.
- `nextShort()`, `nextInt()` and `nextLong()` read whole numbers; `nextFloat()` and `nextDouble()` read real numbers.
- `next()` reads one word, `nextLine()` reads a whole line, and `next().charAt(0)` reads a single character.
- A value that does not match the Scanner method causes an `InputMismatchException` at run time.
- Syntax errors are found by the compiler, runtime errors stop a running program, and logical errors give wrong output without any message.
- A single-line comment starts with `//`; a multi-line comment is enclosed in `/` and `/`; the compiler ignores both.
Questions answered
Define initialisation. Give one example of static and one of dynamic initialisation.
Initialisation is assigning a starting value to a variable when it is declared or before it is first used. Static: `int days = 30;`. Dynamic: `int total = price * qty;`, where the value is calculated at run time.
State one advantage of input through parameters over initialisation.
The same compiled program can be run with different data, entered in the dialog box at the time of execution, without editing the source code.
What is a package? Name the package that contains the Scanner class and the package that is imported by default.
A package is a named group of related classes. `Scanner` belongs to `java.util`; `java.lang` is imported into every program automatically.
Differentiate between `import java.util.Scanner;` and `import java.util.*;`.
The first imports only the Scanner class; the second imports all the classes of the `java.util` package. Both let you use the short name `Scanner`.
Write the statement that creates a Scanner object named `in`, and explain the role of `System.in`.
`Scanner in = new Scanner(System.in);`. `System.in` is the standard input stream connected to the keyboard, so the object reads what the user types.
Write the Scanner statements to read: (a) a 10-digit mobile number, (b) the weight of a parcel in kilograms, (c) the name of a city with a space in it, (d) a single character.
(a) `long mob = sc.nextLong();` (b) `double w = sc.nextDouble();` (c) `String city = sc.nextLine();` (d) `char ch = sc.next().charAt(0);`.
Differentiate between `next()` and `nextLine()`.
`next()` reads a single word and stops at the first white space; `nextLine()` reads the whole line including spaces, up to the Enter key.
Why does the Scanner class need `next().charAt(0)` to read a character?
The Scanner class has no method that reads a character, so a word is read with `next()` and its first character, at position 0, is taken with `charAt(0)`.
Past board exam questions
ICSE 2027 sample paper · 1 mark
Roy wanted to buy the latest mobile phone. He approached a service provider to get a sim card with a 10-digit mobile number. What is the Scanner class method to accept the mobile number?
- Sc.next( )
- Sc.nextInt( )
- Sc.nextLong( )
- Sc.next( ).charAt(0)
Show answer
Sc.nextLong( )
ICSE 2027 sample paper · 1 mark
Fill in the blank to complete the following Java statement: Scanner SC = __________ Scanner(System.in);
- Scanner
- dot
- class
- new
Show answer
new