Model G20 2027 at FLAME University, registrations now open

Data processing in Java | ICSE Class 9 Computer Applications Notes

26 min read

On this page

This note covers data processing, initialisation, parameters, packages, keyboard input with the Scanner class, numeric input methods, text and character input, syntax errors, runtime errors, logical errors, and single-line and multiline comments in Java.

What does data processing mean in a Java program?

Data means the facts or values supplied for a task. Processing means applying instructions to those values to obtain a result. A program is an ordered set of instructions that a computer executes to carry out a task.

Input is the data a program receives; output is the result it produces. Execution means running the program's instructions. Java is a programming language in which those instructions can be written. Source code is the program text written by the programmer.

How do input, processing and output fit together?

Separate the values needed by a calculation from the instructions that operate on them. Supplying data does not itself complete the calculation. Similarly, calculating a result does not itself display that result. Receiving, processing and displaying are distinct jobs.

A variable is a named storage location whose value can change. Its data type determines the kind of value it can hold and the operations permitted on that value. Choosing suitable variables connects input with the processing that follows.

Definition: Data processing in a program involves working on supplied data through instructions to obtain the required result. Correct processing depends on both suitable input and correct instructions.

A method is a named block of instructions belonging to a class. A class specifies the data and behaviour of its objects; an object is an instance created from a class. Methods provide the operations used in the following sections.

What should be decided before writing instructions?

  1. Identify the data the task requires and what each value represents.
  2. Decide whether each value is fixed in the program, supplied to a method, or entered while the program runs.
  3. Choose data types appropriate to the values and the required operations.
  4. Specify the processing and the output that would demonstrate the correct result.

A statement is an instruction in the program. Read statements in execution order when tracing a simple sequence. A trace is a step-by-step record of what executes and how the stored values or output change.

How do initialisation, parameters and input differ?

Initialisation gives a variable its initial value. In the data-supply comparison, initialisation means fixing the data in the program before its execution. A parameter is a variable declared by a method to receive a value when that method is called.

An input stream supplies a sequence of incoming data while the program runs. Keyboard input through Scanner, Java's input-reading class, lets the user enter the required values during execution. The distinction concerns how the program obtains its data.

When is the data supplied?

ApproachTime of supplying the dataHow to recognise it
InitialisationData fixed before executionThe starting value is written in the program.
ParametersData supplied at the time of executionA method receives values when it is called.
Input streamData entered during executionAn input-reading operation obtains the entered value.

These approaches can be used within the same program. Identify how each value is supplied rather than assigning a single input category to an entire program without examining its statements.

Note: “Data before execution” describes when the programmer fixes an initial value in the source. The corresponding initialisation operation takes effect when that part of the program executes; it is not a calculation performed by writing the source.

What is the difference between a parameter and an argument?

An argument is a value or expression supplied in a method call. A method call asks a method to execute. The parameter is the receiving variable in the method's declaration; the argument supplies its value for that call.

An expression combines values, variables or operations to produce a value. An assignment stores a value in a variable. The assignment symbol = directs storage of the value on its right into the variable on its left.

Do not confuse declaring a variable with giving it a starting value. A declaration introduces the variable's name and type. Initialisation supplies its first value, while a later assignment may replace that value. These actions answer different questions about the variable.

What are packages, and how is Scanner made available?

A package groups related Java classes and other program types under a name. A library class is a ready-made class that provides reusable facilities. Scanner is a library class in the java.util package and is used here to read input.

An import declaration lets a program refer to an imported type by its simple name. The full name java.util.Scanner identifies Scanner together with its package. Capitalisation matters: the class name begins with a capital letter, while the package components are lowercase.

What does the import notation mean?

The keyword import introduces an import declaration. A keyword is a word reserved by Java for a defined purpose. The dot . separates components of a qualified name, a name that includes its package information.

The symbol * in this import is a wildcard for accessible types in the named package. The semicolon ; terminates the declaration. This wildcard does not mean multiplication in an import declaration.

Syntax: import java.util.*;

This form permits the use of Scanner by its simple class name. An import does not create a Scanner object, read keyboard data, or perform a calculation. These require separate instructions in the program.

How are packages and objects different?

The new keyword creates an object. A constructor initialises a newly created object. A program uses an object to carry out the operations provided by its class; placing an import above a class declaration does not perform this creation step.

The java.lang package contains fundamental classes and is available without an explicit import. This does not make every other package automatically available by simple class names. In particular, using Scanner by its simple name requires the appropriate import or an equivalent arrangement.

Keep the roles separate when reading a program: the package organises types, the class specifies facilities, the object provides an instance, and a method call performs an operation. This distinction prevents an import statement from being mistaken for an input statement.

How does Scanner receive keyboard input?

System is a Java library class. Its member in, written System.in, is the standard input stream, commonly connected to the keyboard. A member is an element belonging to a class or object, such as a field or method.

A field is a variable declared as part of a class. The dot in System.in selects that member of System. Scanner can be connected to this stream so that its reading methods obtain data supplied through standard input.

What does the Scanner construction expression do?

Round brackets ( ) enclose the arguments supplied to a constructor or method call. In the expression below, System.in is the argument identifying the input source. Scanner is the class whose object is being created.

Syntax: new Scanner(System.in)

This expression creates a Scanner connected to standard input. A program can keep a reference to that object in a variable. A reference is a value through which the program accesses an object.

Creating the Scanner prepares the input facility. A suitable reading method must then be called to obtain data. The method's returned value can be assigned to a variable for use in later statements.

What sequence should an input-based program follow?

  1. Make Scanner available through an import or its qualified class name.
  2. Declare the variables needed to hold the input values and results.
  3. Create a Scanner object connected to the intended input stream.
  4. Call the appropriate reading methods and store the values they return.
  5. Process those values and display the required output.

A return value is the result a method supplies to the calling code. Choose the reading method from the required input type. Entering a value into the keyboard does not change the method's declared return type.

A prompt is a message requesting input. It tells the user what to enter but does not read the value. Conversely, a Scanner reading method does not automatically supply a descriptive prompt. Keep these operations distinct when tracing interaction.

Which Scanner methods read numeric values?

A token, in Scanner input, is a piece of text separated from others by a delimiter. A delimiter marks a boundary between tokens. Scanner's default delimiters are whitespace, including spaces, tabs and line separators.

Numeric reading methods interpret the next token as a value of the specified numeric type. Parsing means interpreting text according to a required format. Selecting a method therefore determines the kind of numeric value the program expects.

Which return type belongs to each method?

An integer is a whole number, which may be positive, negative or zero. Java's short, int and long are integer types with different ranges. A type's range is the set of values it can represent.

A floating-point type represents numbers using a form that supports fractional values. Java's float and double are floating-point types. Their precision describes how much numerical detail they can represent; they need not represent every decimal value exactly.

Reading methodReturned typeExpected kind of input
nextShort()shortAn integer token within the short range
nextInt()intAn integer token within the int range
nextLong()longAn integer token within the long range
nextFloat()floatA token accepted as a float value
nextDouble()doubleA token accepted as a double value

The empty brackets in these method names indicate that these particular calls receive no arguments. They obtain their input from the source already attached to the Scanner object. The spelling and capital letters in each method name matter.

Why must the method match the input?

Input mismatch occurs when the next token cannot be interpreted as the type requested, including an integer outside that type's range. The variable intended to receive the result does not repair a token that the reading method cannot accept.

Rule: Choose the reading method from the required data type, then ensure that the supplied token is valid for that method. Reading numeric-looking text as text and reading it as a number produce different kinds of values.

A numeric Scanner call skips preceding delimiter whitespace. After reading the numeric token, it does not necessarily consume the line separator that follows. This matters when the next operation reads an entire line rather than another numeric token.

How do next(), nextLine() and character input differ?

A String is Java's class for a sequence of characters. A character is an individual text element; the Java type char stores a single UTF-16 code unit. A code unit is one basic storage unit of an encoding. An encoding is a scheme for representing text as codes; UTF-16 is the Unicode encoding used for Java char values.

Unicode provides a system for representing text from writing systems worldwide. Some Unicode characters require more than one UTF-16 code unit. For the basic letters A to Z and a to z, selecting the first code unit gives the first letter.

When should a token or a line be read?

next() returns the next token as a String. With Scanner's default delimiters, it skips leading whitespace and stops at the next whitespace boundary. It is suitable when the required input is a single token.

nextLine() returns the remaining text on the current line, excluding the line separator, and moves past that separator. It preserves spaces within the returned line. It can therefore read text containing more than one word.

OperationResultBoundary used
next()A String containing one tokenThe next delimiter after the token
nextLine()A String containing the rest of the current lineThe current line's ending
next().charAt(0)The first char of the next tokenA token is read before its first position is selected.

How does character selection work?

charAt is a String method that selects a char at a specified position. An index is a numbered position in a sequence. Java String indexing starts at 0, so charAt(0) selects the first code unit.

Syntax: next().charAt(0) first reads a token with next(), then calls charAt(0) on the returned String. The dot links the second operation to the first operation's result.

This expression combines a Scanner method and a String method. It reads a token, then keeps its first char as the expression's result; it does not return the whole token.

After a numeric read, nextLine() may return an empty String if the remaining part of that line contains no characters before the line separator. Consume a leftover line ending only when the input layout requires it; an extra line read can otherwise discard useful data.

How can syntax errors be recognised and corrected?

Syntax is the set of rules governing how instructions must be written. A syntax error breaks those rules. A compiler checks and translates source code before it is run.

Compilation is the process of compiling that source code. Syntax errors must be corrected before the affected program can be compiled successfully. A compiler's diagnostic is a message that helps identify a detected problem.

Which written details deserve attention?

Check statement terminators, paired brackets and the arrangement of declarations and calls. Curly braces { } enclose a block, a grouped set of statements or declarations. An opening bracket must have its corresponding closing bracket in the appropriate position.

The numeric expression (10 + 12) has matching round brackets. Here + means addition and the brackets group the addition. The fragment (7 + 11 lacks its closing bracket and is syntactically incomplete.

These written expressions show why the presence of sensible numbers is not enough. The program's grammatical structure must also be complete. A compiler cannot treat a missing bracket as though the programmer had supplied it.

How should a compilation problem be investigated?

  1. Read the diagnostic and identify the part of the source to which it refers.
  2. Inspect that statement and the nearby structure, including brackets and terminators.
  3. Correct the actual written defect without changing unrelated processing instructions.
  4. Compile again to check whether the defect and any resulting diagnostics have been removed.

Note: Not every compilation error is a syntax error. A program can obey grammatical rules yet fail a type or name check. “Detected during compilation” identifies the stage; “syntax error” identifies a particular kind of defect.

Debugging means identifying and removing errors from a program. Removing syntax errors is part of debugging, but successful compilation does not prove that all inputs will be accepted or that the program will calculate the intended result.

After a correction, continue to the execution and result checks. Treat a successful compilation as evidence that this stage succeeded, not as evidence that every later stage must also succeed.

How are runtime errors different from logical errors?

A runtime error occurs while a program is executing. A logical error means that the instructions implement the intended task incorrectly. A program with a logical error may run and display output, yet the output does not satisfy the task.

An exception is Java's mechanism for signalling certain abnormal conditions during execution. If an exception is not handled, it can interrupt normal execution. An input mismatch while reading numeric data is one relevant source of such an exception.

How do the error categories compare?

Error categoryWhat is wrong?What should be checked?
Syntax errorThe written form breaks language grammar.Statement structure, bracket matching and terminators
Runtime errorAn operation encounters a problem during execution.The failing operation and the input or values reaching it
Logical errorThe implemented steps do not give the required result.The intended calculation, execution trace and expected output

For Java integer division, a zero divisor causes an arithmetic exception. A divisor is the number by which another number is divided. Do not generalise that rule unchanged to Java floating-point division, whose zero-division behaviour is different.

Why can a wrong calculation still run?

The arithmetic mean of two values is their sum divided by two. In 10 + 12 / 2, the slash / means division. Division has higher precedence, or priority of grouping, than addition.

Worked example 1. Find the arithmetic mean of 10 and 12. Explain why the expression 10 + 12 / 2 gives the wrong result, and correct it.

Answer: Division is evaluated first in the original expression: 12/2=612 / 2 = 6, then 10+6=1610 + 6 = 16. The expression is grammatically valid, but 16 is not the intended mean, so this is a logical error.

Group the addition first: 10+12=2210 + 12 = 22. Then divide the sum by two: 22/2=1122 / 2 = 11. The corrected expression is (10 + 12) / 2, and the arithmetic mean is 11.

Rule: Compare actual output with the result required by the task. A displayed answer is evidence that output occurred; it is not sufficient evidence that the program's logic is correct.

When tracing a logical error, record the data, evaluate each operation in the order prescribed by the expression, and compare the resulting value with the intended calculation. Correct the expression or processing steps responsible for the difference.

Error classification should follow the actual defect. A numeric input problem should not be labelled a syntax error merely because the input is unsuitable. Likewise, an incorrect formula need not cause an exception.

How do single-line and multiline comments work?

A comment is explanatory text included in source code for human readers. Comments are non-executable: they explain the purpose of instructions, the meaning of data, or the input and output requirements without carrying out those operations.

Java supports single-line comments and multiline comments. Comment markers tell the language processor which text belongs to a comment. The markers must be distinguished from ordinary punctuation used in executable expressions.

What does each comment marker mean?

The symbol // begins a single-line comment. The comment extends to the end of that line. It can occupy a line by itself or follow an executable statement whose explanation is being supplied.

The opening marker /* begins a multiline comment; the closing marker */ ends it. The enclosed comment can span lines. A line break alone does not close this kind of comment.

Syntax: // introduces a comment continuing to the line ending. The paired markers /* and */ enclose a comment that can continue across line endings.

FeatureSingle-line commentMultiline comment
Opening marker///*
End boundaryThe line endingThe closing marker */
ExecutionComment text is not executed.Comment text is not executed.
Typical purposeA brief explanation beside or above a statementAn explanation that may occupy several lines

What makes a comment useful?

Explain the meaning or purpose of the code instead of merely repeating its appearance. An input comment can state what data the program expects. A processing comment can identify the calculation. An output comment can explain what the displayed result represents.

Changing an explanatory comment does not correct an incorrect executable formula. The instructions themselves must be corrected. An outdated comment can also mislead a reader, so check that explanations still match the program after edits.

Comment markers inside a quoted String literal are text, not comment delimiters. A literal is a value written directly in source code; double quotation marks delimit a String literal. Identify the context before deciding whether punctuation begins a comment.

How should an input-processing program be checked?

Checking a program means comparing its actual behaviour with its intended behaviour. Testing runs the program with selected input to examine that behaviour. Debugging then investigates and corrects any defects revealed by compilation, execution or the resulting output.

Start with the task itself: what values are supplied, what calculation is required, and what result should be produced? An input method can read data successfully even when the later processing is wrong. Check each stage separately.

What should a trace record?

  1. Record the supplied values, whether fixed, passed as arguments, or entered through an input stream.
  2. Identify the receiving variables and the types returned by the reading methods.
  3. Track the values used by each expression and the order of operations.
  4. Record assignments so that later statements use the updated values.
  5. Record the displayed result and compare it with the independently expected answer.

System.out denotes the standard output stream, commonly directed to the screen. Its print() method displays supplied content without adding a line ending; println() displays content and then ends the output line.

Output calls are separate from input calls. A prompt may be displayed before a Scanner read, and a calculated result may be displayed afterwards. Neither the presence of a prompt nor the presence of an answer establishes that the intervening data processing was correct.

Which checks prevent common mistakes?

Check whether text should be read as a token or a whole line. Check whether character extraction is intended. For numeric input, check that the chosen reading method accepts the required type and that its result is stored appropriately.

When input methods are mixed, follow the Scanner's position in the input. A numeric token read and a line read use different boundaries. Explaining that position is more reliable than adding extra reads without considering what data they consume.

Finally, classify a discovered error by its cause, correct that cause, and repeat the relevant check. Keep comments consistent with the corrected instructions. A complete review considers source structure, input handling, processing and output together.

Glossary

  • Initialisation — Giving a variable its initial value before that value is used in processing.
  • Parameter — A receiving variable declared by a method for data supplied when the method is called.
  • Argument — A value or expression supplied to a method or constructor when it is called.
  • Package — A named grouping of related Java classes and other program types.
  • Input stream — A source supplying a sequence of incoming data during program execution.
  • Scanner — A Java library class that reads and interprets input using suitable reading methods.
  • Token — A piece of input text separated from other pieces by delimiters.
  • Delimiter — A boundary recognised by Scanner to separate one input token from another.
  • Index — A numbered position used to select an element within a sequence.
  • Syntax error — A defect in the written program that breaks the language's grammatical rules.
  • Runtime error — A problem encountered while a program is executing its instructions.
  • Logical error — A defect in processing instructions that makes the program behave incorrectly.
  • Debugging — The process of identifying and removing errors from a computer program.
  • Comment — Non-executable explanatory text added to source code to help human readers understand it.

Common errors and misconceptions

  • Misconception: Initialisation, parameters and keyboard input supply data in the same way. Correct: They distinguish data fixed before execution, supplied to a method at execution, and entered through an input stream during execution.
  • Misconception: Importing java.util creates a Scanner and reads input. Correct: An import makes class names available. Object creation and reading-method calls perform separate jobs in the program.
  • Misconception: nextInt() accepts any value that looks numeric. Correct: The token must be accepted as an int value, including being within the int range. Choosing a receiving variable cannot repair invalid input.
  • Misconception: next() and nextLine() return the same text. Correct: next() reads a token; nextLine() reads the remainder of the current line, including spaces within that remainder.
  • Misconception: next().charAt(0) is a single Scanner character-reading method. Correct: next() returns a String, then the String method charAt(0) selects its first char.
  • Misconception: Successful compilation proves that the program is correct. Correct: The program may still encounter a runtime error or produce incorrect output because its processing contains a logical error.
  • Misconception: Writing the correct formula in a comment repairs a wrong calculation. Correct: Comments explain code but are non-executable. The executable expression must be corrected for the result to change.

Exam-style questions with model answers

Q1. Define initialisation and a parameter, distinguishing how each supplies data to a Java program. [2 marks]
  1. Initialisation gives a variable its initial value; in this comparison, the data is fixed in the source before execution.
  2. A parameter is a receiving variable declared by a method; its value is supplied when that method is called.
Q2. Explain the separate roles of import java.util.*;, new Scanner(System.in), and a call to nextInt() on that Scanner. Assume the next input token is a valid int. [3 marks]
  1. The import declaration allows accessible types in java.util, including Scanner, to be referred to by their simple names. Importing does not create an object.
  2. The construction expression creates a Scanner connected to System.in, the standard input stream. System.in identifies the source from which that Scanner will read.
  3. The nextInt() call reads the next token as an int and returns the value. A program can store that returned value for later processing.
Q3. A program needs to read values of these Java types: short, int, long, float and double. Name the Scanner method appropriate to each type and explain what it returns. [5 marks]
  1. For short, use nextShort(). It interprets the next token as a short integer and returns that value, provided the token is valid for the type.
  2. For int, use nextInt(). It reads an integer token accepted within the int range and returns an int value to the calling code.
  3. For long, use nextLong(). It interprets the next token as a long integer and returns a long value for use in the program.
  4. For float, use nextFloat(). It interprets an accepted numeric token as a floating-point value and returns the result with the Java type float.
  5. For double, use nextDouble(). It interprets an accepted numeric token as a floating-point value and returns the result with the Java type double.
Q4. Compare next() and nextLine() using Scanner's default whitespace delimiters. Explain why nextLine() immediately after nextInt() may return an empty String when the entered integer is followed immediately by a line separator. [3 marks]
  1. next() skips leading delimiter whitespace and returns the next token as a String. It stops at the token boundary rather than reading all remaining words on the line.
  2. nextLine() returns the remaining characters on the current line, excluding the line separator, and advances past that separator. Spaces within the returned text are preserved.
  3. nextInt() reads the integer token but leaves the following line separator. Here no characters remain before that separator, so the subsequent nextLine() returns an empty String.
Q5. A Scanner has a non-empty token available. Explain next().charAt(0): identify the two methods, the intermediate type, and the meaning of the index 0. [4 marks]
  1. next() is the Scanner method that reads the next token from the input using the Scanner's delimiter rules.
  2. Its return value is a String containing that token. This String becomes the object on which the following method is called.
  3. charAt is a String method that returns the char at the specified index. It is not a second Scanner reading method.
  4. The index 0 denotes the first position. Therefore the complete expression returns the first char of the token that next() has read.
Q6. Classify and explain these three defects: the incomplete expression (7 + 11; Java integer division with a divisor equal to zero during execution; and using 10 + 12 / 2 to find the arithmetic mean of 10 and 12. Give the corrected mean expression and result. [6 marks]
  1. The incomplete expression has a syntax error because its opening round bracket has no matching closing bracket. The written structure must be corrected before successful compilation.
  2. Integer division with a zero divisor causes an arithmetic exception during execution. This is a runtime problem arising from the value used by the division operation.
  3. Using 10 + 12 / 2 for the mean is a logical error. The expression can be evaluated, but it does not describe the intended calculation.
  4. Division takes precedence over addition, so 12 / 2 is evaluated as 6 and adding 10 gives 16. That result is not the required mean.
  5. The corrected expression is (10 + 12) / 2. Its brackets group the addition so that the sum, rather than just the second value, is divided by two.
  6. The correct arithmetic mean is 11. Comparing this expected result with the original result of 16 exposes the processing error even though the original expression is syntactically valid.
Q7. Explain how a single-line comment and a multiline comment begin and end in Java. [2 marks]
  1. A single-line comment begins with // and continues to the line ending; the comment text is not executed.
  2. A multiline comment begins with /* and ends with */; the enclosed non-executable text may extend across several lines.

Key takeaways

  • Data processing connects input values, instructions that operate on those values, and output showing the result.
  • Initialisation fixes starting data in the source, parameters receive supplied arguments, and input streams provide data during execution.
  • Importing Scanner, creating its object, and calling a reading method perform different jobs in an input-based program.
  • Select numeric reading methods according to the required Java type, and supply tokens that those methods can accept.
  • Use next() for a token and nextLine() for the remainder of a line, taking care when mixing reading methods.
  • The expression next().charAt(0) reads a token as a String and then selects its first char.
  • Syntax errors break grammar, runtime errors arise during execution, and logical errors make the processing fail its intended purpose.
  • Comments explain source code for readers; checking and correcting executable instructions determines whether the processing produces the required result.

Test yourself

Which package contains Scanner, and what is its qualified class name?

Scanner belongs to java.util. Its qualified class name is java.util.Scanner.

Does import java.util.*; read keyboard input?

No. It makes accessible types available by simple names; object creation and reading-method calls are separate operations.

What types do nextLong() and nextDouble() return?

nextLong() returns a long integer value; nextDouble() returns a double floating-point value.

Why can nextLine() preserve spaces within the text it reads?

It reads the remaining text up to the line separator, instead of stopping at a whitespace token boundary.

In next().charAt(0), which class provides charAt?

String provides charAt. The method acts on the String returned by Scanner's next() call.

Can a program compile successfully and still contain a logical error?

Yes. Its written structure may be acceptable while its instructions calculate a result that does not meet the task.

What closes a multiline comment, and does a line break close it?

The marker */ closes a multiline comment. A line break by itself does not close it.

Why should a displayed result be compared with an independently expected answer?

Displaying output shows that an output operation occurred; comparison is needed to check whether the processing was correct.