Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Computer Application IX InputInJava

The document discusses various topics related to input in Java programs including comments, packages, Scanner class, input/output streams, and errors. It provides details on using single-line and multi-line comments, built-in Java packages and creating custom packages, taking user input using the Scanner class and its methods, common input/output streams, and types of errors in Java programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Computer Application IX InputInJava

The document discusses various topics related to input in Java programs including comments, packages, Scanner class, input/output streams, and errors. It provides details on using single-line and multi-line comments, built-in Java packages and creating custom packages, taking user input using the Scanner class and its methods, common input/output streams, and types of errors in Java programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Input in Java

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.

/* Used for multiline comments ---


------------------- */

/** Same as multiline comments


used for documentation */

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.

Advantages of packages: Packages are very useful. Such as-


 It implements encapsulation.
 Classes of other packages can be reused using import keyword.
 Classes can be managed effectively using packages.
 We can create classes with the same name in different packages.

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)

Some commonly used streams are-


 System.in - Standard input stream (connected to keyboard).
 System.out - Standard output stream (connected to monitor).
 System.err - Standard output stream for error message (connected to monitor).

The Scanner breaks its input into tokens using a delimiter (space is default delimiter).

Java Scanner class provides the following methods to read data:

Method Description

nextInt() It is used to scan the next token of the input as an integer.

nextFloat() It is used to scan the next token of the input as a float.

nextDouble() It is used to scan the next token of the input as a double.

nextByte() It is used to scan the next token of the input as a byte.

nextLine() It is used to scan the complete line as string

nextBoolean() It is used to scan the next token of the input into a boolean value.

nextLong() It is used to scan the next token of the input as a long.

nextShort() It is used to scan the next token of the input as a Short.

next() It is used to scan the next token of the input as a string.

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.

A program to take input using Scanner class:


import java.util.*;
class UserInput
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print("Enter Your Name- ");


String nm= sc.nextLine();

System.out.print("Enter Your Marks- ");


int m= sc.nextInt();

System.out.println("Hello " + nm + ", you got " + m + " marks. " );


}
}

Output of above program:

Enter Your Name- Rakesh Ranjan


Enter Your Marks- 85
Hello Rakesh Ranjan, you got 85 marks.

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

You might also like