Inputs in Java
Errors: are basically programming problems which we face while solving a problem.
There are different types of errors:
1) Syntax Error or Compile-Time Error: are the errors which immediately detected by the javac compiler during compilation.
The common mistakes that causes syntax or compile time errors are:
1) Missing semicolon
2) Misspelling of identifiers and keywords
3) Use of undeclared variables.
4) Incompatible types in assignments or initializations
5) Use of = in place of == operator
6) Missing braces {} in classes and methods.
and many more
2) Run Time Error: the error that appears during the run or execution time of a program other than the syntax error is called runtime error.
The common mistakes that causes runtime errors are:
1) Dividing a number by zero.
2) Passing a parameter that is not in a valid range or value for method.
3) While entering data during the program execution, entering an integer into a string data type will result in an error.
4)Attempting to use a negative size for an array
5) Due to invalid type casting
and many more.
3) Logical or Semantic Error: the error that does not appear during the compilation of program or during the execution/runtime of a program is called logical error. Logical error only can be found in wrong value output of a program which is not desired.
-------------------------------------------------------------------------------------------------
Inputs in java can be taken using different approaches:
1)By using a Function Argument (in BlueJ IDE system)
2)By using a InputStreamReader Class
3)By using a Scanner class
1) By using a function argument: we can take values from the user during the void main() function call in bluej.
For example: public static void main(int a,int b)
//WAP to accept three numbers from the user. Display the sum and average of them.
class average
{
public static void main(int a,int b,int c)
{
int sum=a+b+c;
double avg=sum/3.0;
System.out.println("The sum of three numbers are:"+sum);
System.out.println("The average of three numbers are:"+avg);
}}
2) By using an InputStreamReader Class:
a) First we have to import the package import java.io.*;
b) Then we have to write public static void main(String args[])throws IOException
c) Then we have to declare two lines:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
or we can also write
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
d) There are different methods to take input for each data type:
i) To accept integer:
int n = Integer.parseInt(br.readLine());
ii)To accept long:
long n = Long.parseLong(br.readLine());
iii) To accept byte:
byte n = Byte.parseByte(br.readLine());
iv) To accept short:
short n = Short.parseShort(br.readLine());
v) To accept double:
double n = Double.parseDouble(br.readLine());
vi)To accept float:
float n = Float.parseFloat(br.readLine());
vii)To accept String(a sentence)
String n = br.readLine();
viii) To accept a word
String n = br.read();
ix) To accept a character
char n = (char)(br.read()); or char n=br.read().charAt(0);
3) To accept inputs using Scanner Class:
a) First declare the package: import java.util.Scanner; or import java.util.*;
b) Declare Scanner sc=new Scanner(System.in); after public static void main()
c) To take input as byte: byte a = sc.nextByte();
d) To take input as short: short a = sc.nextShort();
e) To take input as int: int a = sc.nextInt();
f) To take input as long: long a = sc.nextLong();
g) To take input as float: float a= sc.nextFloat();
h) To take input as double: double a = sc.nextDouble();
i) To take input as String(sentence): String s = sc.nextLine();
j) To take input as String(word): String s = sc.next();
k) To take input as char: char ch = sc.next().charAt(0);
Comments: are non-executable statements that they do not participate in programming,they do not change the logic of program. It is used to explain what concept a programmer has used to develop the code.
There are three types of comments:
1)Single line comment:
It is written like this:
// this is a single line comment.
2)Multi line comment:
It is used to written a paragraph or explaining steps in multiple points.
/*
This is an
example of
multi line comment
*/
3)Documentation comment: is used to written detailed instructions about the thought process of
writing a program in details. It contains author name, class name, method name,version,date of creation
and etc.
/**
Author: Shreyan Nag
Version: 0.0.1
Date: 28/04/2025 18:41
*/