Input in Java
Input in Java
8. What is the different type of errors that may occur in a Java program?
Errors in a program in Java can broadly be classified into three categories: • Syntax errors • Logical errors • Run-time
errors
11. What are comments? Name the different types of comments used in Java.
A comment is a programmer-readable explanation or annotation in the source code of a computer program.
The different types of comments in Java are:
• Single line comment
• Multiline comment
1
13. Explain different input types of Java Sanner class.
Method Description Example
nextShort() Reads a short value from the user Scanner sc = new Scanner (System.in);
short a=sc.nextShort();
System.out.print(a);
nextInt( ) Reads a int value from the user Scanner sc = new Scanner (System.in);
int a=sc.nextInt();
System.out.print(a);
nextLong( ) Reads a long value from the user Scanner sc = new Scanner (System.in);
long a=sc.nextLong();
System.out.print(a);
nextFloat ( ) Reads a float value from the user Scanner sc = new Scanner (System.in);
float a=sc.nextFloat();
System.out.print(a);
nextDouble( ) Reads a double value from the user Scanner sc = new Scanner (System.in);
double a=sc.nextDouble();
System.out.print(a);
next( ) It read input from the input device till the Scanner sc = new Scanner (System.in);
space character. String a=sc.next(); // computer science
System.out.print(a); // computer
nextLine( ) It read input from the input device till the Scanner sc = new Scanner (System.in);
line change. String a=sc.nextLine(); // computer science
System.out.print(a); // computer science
next ( ) .charAt(0) next() function returns the next Scanner sc = new Scanner (System.in);
token/word in the input as a string and char a=sc.next().charAt(0); // computer science
charAt(0) function returns the first System.out.print(a); // c
character in that string,
2
16. Difference between the next() and the nextLine() method:
next() nextLine()
It read input from the input device till the space It read input from the input device till the line change.
character.
It cannot read those words having space in it. It can read those words having space in it.
Syntax to scan input: Syntax to scan input:
Scanner.next() Scanner.nextLine()