0% found this document useful (0 votes)
8 views4 pages

05 InputInJava10

The document provides an overview of input methods in Java, detailing three primary ways to receive input: initializing values, using main() method parameters, and utilizing the Scanner class. It includes example programs for each method, along with explanations of class and method definitions, various types of statements, print statements, error types, and comments in a program. Additionally, it emphasizes the structure of Java programs and the importance of correct syntax and error handling.

Uploaded by

rafeekher.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

05 InputInJava10

The document provides an overview of input methods in Java, detailing three primary ways to receive input: initializing values, using main() method parameters, and utilizing the Scanner class. It includes example programs for each method, along with explanations of class and method definitions, various types of statements, print statements, error types, and comments in a program. Additionally, it emphasizes the structure of Java programs and the importance of correct syntax and error handling.

Uploaded by

rafeekher.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ICSE Computer Applications 1 Input In Java

5. Input in Java
Introduction
Input in general means giving values to a program. There are three ways of inputs.
1. Initialising values: Giving values when program writing: E.g.: int a = 5;
2. Input using main() method parameters: E.g.: void main(int a)
3. Input using Scanner class methods: E.g.: int a = sc.nextInt();
1. Basic Structure of a Program
[import statement]
class definition
Note: The [ ] means the import statement is optional, i.e., if it is required use it, else no need.
2. Various Kinds of Inputs
1. Input By Initializing Values: An Example Program
public class Square
{
public static void main()
{
int n = 5;
int sq=n*n;
System.out.println("Square = "+sq);
}
}
Note: In this program no input when running the program; so no need of import statement.
Description of the Above Program
Class Definition
The whole program part is a class definition. A class definition includes method definition,
and a method includes statements in it.
Syntax of Class Definition
[access specifier] class class_name
{
method definition
{
statements;
}
}
Note: The public keyword is an access specifier. In simple programs it is not necessary to
write public. Instead of public class Sum we can write class Square
Method Definition
Syntax of main() Method Definition
access specifier modifier return_type method_name
{
statements;
}
Note: The public and void are nececessary to deffine the main() method definition
Statements
The statements in the above program are intitaialization statement, process statement, and
output/print statement.
Syntax of initaialization statement
data_type variable_name = value/variable/expression; E.g.: int n = 5;
Some Other Initialization Statements
String name = “Anu”; For words and sentences (strings)
char c = ‘A’; For a single character
long mob = 9446748197L; For 10 digit integers (example: mobile numbers)
double m = 98.5; For decimal pointed numbers
Raju Xavier | 9446748197 | www.rajuxavier.com 1 Input in Java
ICSE Computer Applications 2 Input In Java
2. Input Using main() Method Parameter: An Example Program
public class Square
{
public static void main(int n)
{
int sq=n*n;
System.out.println("Square = "+sq);
}
}
Description of the Above Program
main(int n)
The int n in the bracket of main() is the parameter. When the program starts a method call
window appears with a text box. Input an integer in the text box and click Ok. Square of the
n will be calcutated and output is printed on the Terminal Window. Input can be done only
when the program starts; during execution parameter input is not possible.
Some Other Parameter Declarations
main(String name, int cl, char div, long mob, double m1, double m2)
Each variable should be declared individually with a comma separator. Note the two double types.
3. Input Using a Scanner Class Methods: An Example Program
import java.util.*;
public class Square
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer");
int n = sc.nextInt();
int sq=n*n;
System.out.println("Square = "+sq);
}
}
Description of the Above Program
Import Statement
The import java.util.*; is known as an import statement.
The import is a keyword. It means connect or link.
The java.util is a package. This packge contains classes like Scanner. The Scanner class
has some methods to input values at runtime.
The * represents all classes in a package including Scanner.
The import java.util.*; connects our program to the Scanner class in java.util package.
Statements For Runtime Input
Scanner sc = new Scanner(System.in);
It is an object creation statement. An object named sc is created for Scanner class.
System.out.println("Enter an integer");
This print statement is a user request to input an integer. It is known as user prompt.
int n = sc.nextInt();
It is a runtime input statement. The nextInt() method of Scanner class with refrence of sc
object accepts an integer number from the user through the keyboard and stores in a variable
named n which is int type.
Some Runtime Input Statements
String name = sc.nextLine(); For words and sentences (strings)
String name = sc.next(); For a single word
char c = sc.next().charAt(0); For a single character
int n = sc.nextInt(); For integers less than 10 digit integers
long mob = sc.nextLong(); For 10 digit integers (example: mobile numbers)
double m = sc.nextDouble(); For decimal pointed numbers – double type
float m = sc.nextFloat(); For decimal pointed numbers – float type
Use nextByte(), nextShort(), nextBoolean() for byte, short, boolean types respectively.
Raju Xavier | 9446748197 | www.rajuxavier.com 2 Input in Java
ICSE Computer Applications 3 Input In Java
3. Various Definitions, Prototypes and Statements
Two Basic Defenitions
Class definition and method definitions are two basic definitions in Java.
Class definition example:
public class Sum
{ }
Method definition example:
public static void main()
{ }
Two Prototypes
A prototype is the first line of a class definition or method definition. Examples:
Class prototypes: public class Sum
Method definition: public static void main()
Basic Statements
A statement is a group of tokens that convey a complete instruction to the compiler.
E.g.: int a = 5;
1. Import statement
It is used to link classes in a predefined package to a running program.
E.g.: import java.util.*;
2. Variable declaration statement
It is used to declare variable before using it. No value is assigned here.
E.g.: int a;
3. Initialization statement
It is used to initialize a value to a variable.
E.g.: int a = 1;
4. Assignment statement
Assigning a value to a previously declared variable is an assignment statement.
E.g.: a = 5;
5. Object creation statement
Creating an object class as its data type is an object creation statement.
E.g.: Scanner sc = new Scanner(System.in);
The sc is an object and Scanner is a class as data type.
6. Input statement
It is used to read/accept values at run time from user.
E.g.: String a = sc.nextLine();
7. Print statements
The built-in methods to get output are the print statements. E.g.:
System.out.println(“Hello”);
System.out.print(“dear”);
Name the Statements / Definition
1. double m = 99.5; 10. public class Number
2. String s; {
3. s = “Anu”; public static void main()
4. Scanner sc=new Scanner(System.in); {
5. char c = sc.next().charAt(0); System.out.println("Hello");
6. System.out.println(a); }
7. public static void main() }
8. public class Number
9. public static void main()
{
System.out.println("Hello");
}
Answer:
1. Initialization statement 6. Print statement
2. Declaration statement 7. Method prototype
3. Assignment statement 8. Class prototype
4. Object creation statement 9. Method definition
5. Input statement 10. Class definition
Raju Xavier | 9446748197 | www.rajuxavier.com 3 Input in Java
ICSE Computer Applications 4 Input In Java
4. Two Kinds of Print Statements
There are two kinds of print statements in Java.
1) System.out.println()
2) System.out.print()
Example:
System.out.print("A");
System.out.println("B");
System.out.print("C");
Output:
AB
C
5. Three Kinds of Errors
Compile time error (Syntax error)
The error happens when violating the rules of a programming language.
E.g.: Public class Sum. A keyword (public) should be in lowercase – this rule is violated.
Runtime error
No syntax error but an error happens during running the program and execution stops.
E.g.: int n = 5/0; When an integer is divided by 0 the program stops when this statement executes.
Logical error
No syntax error and run time error but the output is wrong answer.
E.g.: The int sum=5-3, results the difference 2, not the expected sum which is 8.
Answer the following:
Find the type of error in the following and write breif explanations:
1. int square=2*n; 2. int a=6,b=0; int n=a/b; 3. Int n=2;
Answer:
1. Logical error. To find square it requires n*n not 2*n.
2. Runtime error. Because the devisor is 0. An integer cannot be divided by 0.
3. Syntax error. The first letter is in caps, all letters should be small letters
6. Comments in a Program
Comments in a program are used to describe or remark, a program or program part to the
reader. There are three comments. These are:
1. Single line comment: //
2. Multi-line comment: /* */
3. Documentation comment: /** */
Example for Using Various Comments
/* The following program is used to find difference of two integers
and display the output with heading */
public class Difference //Class definition
{
public static void main()
{
int a=5,b=3; //It is an initialization statement
int d=a-b; //To find difference
System.out.println(“Difference=”+d); //To display output
}
}
/** Written by Hima
Year: 2018 */
Answer the following:
Choose the valid comments:
1. \\to find sum
2. //input statement
3. /* Program to reverse an integer /*
4. /*Example program to convert
meter to centimeter*/
5. /**Version 5.01*/
Answer:
2. //input statement
4. /*Example program to
convert meter to centimetre*/
5. /**Version 5.01*/
Raju Xavier | 9446748197 | www.rajuxavier.com 4 Input in Java

You might also like