0% found this document useful (0 votes)
5 views5 pages

5 Input in Java

The document provides an overview of input methods in Java, detailing comments, assignment statements, function arguments, and the Scanner class for user input. It also explains testing and debugging processes, including types of errors such as syntax, logical, and runtime errors. Additionally, it includes example programs demonstrating how to add two numbers using different input methods.

Uploaded by

21sonirishit
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)
5 views5 pages

5 Input in Java

The document provides an overview of input methods in Java, detailing comments, assignment statements, function arguments, and the Scanner class for user input. It also explains testing and debugging processes, including types of errors such as syntax, logical, and runtime errors. Additionally, it includes example programs demonstrating how to add two numbers using different input methods.

Uploaded by

21sonirishit
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

Name: _____________________________ Grade : IX Div.

: ____________________

Roll No. : ______ Date:______________ Term : I Worksheet No: 5

Subject: Computer Applications Topic: Input in Java

Comments:
The non-executable statements in Java programming that enable the users to
understand the program logic are called Comments.

Types of Comments:
1.​ Single line Comment (//)
When a Comment can be expressed in a single line.

2.​ Multi line Comment (/*………………..*/)


Comments used to explain the logic of the program in detail by using
more than one line.

3.​ Documentation Comment (/**…………………………..*/)


It is used when the user may need to include some information about
himself and his program. It includes the information to be viewed by
other users who may need to execute your program.

Ways to accept Input in Java Programming

1.​ By Using Assignment statement

2.​ By using Function Argument

3.​ By using Scanner class

Page 1
Input using Assignment statement
A statement that stores a value of a variable is known as an assignment
statement.
Example:
int a =3;
float f=3.5;
char ch= ‘A’;
String st= “Grade 9”;

Input using Function argument


This is one of the methods to accept the value from the user at the time of
execution of the program. The variables whose values are to be input must be
provided as arguments of main( ) function
Example: public static void main(int a, int b)

Input using Scanner class


The Scanner class is defined within the java package [Link]. You must import
the [Link] package to use the Scanner class.

Statement to import util package


import [Link].*;

Statement to create Scanner object


Scanner sc = new Scanner ([Link]);

Class keyword Constructor


Object

The Scanner class basically works on the principle of Tokens. A token is a series
of characters that ends with a delimiter. We use whitespace as a delimiter.

Page 2
Methods to read tokens from Scanner Object

●​ nextShort( ) – It reads the next token of the short data type.


●​ nextInt( ) – It reads the next token of int data type.
●​ nextFloat( ) – It reads the next token of float data type.
●​ nextLong( ) – It reads the next token of the long data type.
●​ nextDouble( ) – It reads the next token of the double data type.
●​ next( ) – It reads the next token of the String data type which is a set of
characters between two whitespaces.
●​ next( ).charAt(0) – It reads the next token of a single character.
●​ nextLine( ) – It reads the token of a String type.

Testing and Debugging

Testing Debugging

Testing is a process in which a program Debugging is a process in which the


is validated. errors in the program are removed.

Testing is complete when all the Debugging is complete when there are
desired verifications against no errors and the program is ready for
specifications have been performed. execution.

Types of Errors

There are 3 types of Errors:

1.​ Syntax Errors: They occur when the rules or the grammar of the
programming language are not performed. They typically involve
incorrect punctuation, incorrect word sequences, undefined terms or
incorrect use of terms.
Example: cannot find symbol – variable p, semicolon missing.

Page 3
2.​ Logical Error: A logical error is concerned with an error in planning the
program’s logic. The computer actually does not know the error has been
made. It simply follows the instructions and executes the results, but the
output may not be correct. The problem is that the logic being given does
not produce the desired results.
Example: If you want to find the average of 3 numbers but you add 2
numbers and divide by 3.

3.​ Run Time Error: Sometimes, you find the program is not producing the
desired results, even after errors are removed and the program is
compiled. These errors may occur due to incorrect mathematical tasks or
by entering incorrect data values at the time of input.
Example: Dividing a number by 0, trying to find out square root of a
negative number.

Syntax Error Logical Error

It is the grammatical error in the


It is the error in the program logic.
statement.
The program is unable to give the The program doesn’t give the desired
output. output.

Program to add two numbers using Assignment statement


public class Addition
{
​ public static void main(String args[ ])
​ {
​ ​ int a=10,b=20;
​ ​ int c=a+b;
​ ​ [Link](“The addition of two numbers is ”+c);
​ }
}

Page 4
Program to add two numbers using Function Argument
public class Addition
{
​ public static void main(int a, int b)
​ {
​ ​ int c=a+b;
​ ​ [Link](“The addition of two numbers is ”+c);
​ }
}

Program to add two numbers using Scanner class


import [Link].*;
public class Addition
{
​ public static void main(String args[ ])
​ {
​ ​ Scanner sc = new Scanner([Link]);
​ ​ int a,b;
​ ​ [Link](“Enter two numbers”);
​ ​ a = [Link]( );
​ ​ b = [Link]( );
​ ​ int c=a+b;
​ ​ [Link](“The addition of two numbers is ”+c);
​ }
}

Page 5

You might also like