Write down the syntax to accept a fractional value ‘n’ in double data type through Scanner class.
Answers
The syntax to accept a fractional value n in double type (using Scanner class) will be –
Scanner obj = new Scanner(System.in);
double n = obj.nextDouble();
obj.close();
Note: The Scanner class must be imported before creating the objects.
The Scanner class is used to take input from the user. It is present in utility package. We can import scanner class in our program by writing –
> import java.util.Scanner;
After importing scanner class, we have to create it's object.
> Scanner obj = new Scanner(System.in);
To take a fractional value of type 'double' as input, we can use the nextDouble() function.
> double n = obj.nextDouble();
As our problem is solved, close the scanner.
> obj.close();
Various method of Scanner class:
- nextInt(): Used to take integer as input.
- nextFloat(): Used to text floating type value as input.
- nextDouble(): Used to take double type value as input.
- nextLong(): Used to take a long type value as input.
- next(): Accepts the next token which is a string.
- nextLine(): Used to take a line of text as input.
Answer:
First you need to know that what is scanner class:-
- It is an easier but slower method of inputting statements.It is in the JDK package 1.5 and above.
- The scanner class is easier to use but as prescribed by the boards you should use InputStreamReader and BufferedReader class and hardly any question of scanner class comes in boards.
- Also, note that just like other inputting we have to give the values at run time.
- Also,note that in the end close the scanner class using
<object name>.close();
- First you need to import a package util or you can import only scanner class. The scanner class is a member of a utility functions in Java. Hence ,it is imported by
import java.util.*;(whole class)
Or
import java.util.Scanner(Only Scanner class)
- The syntax of various operations
First create an obj of the class
Syntax:-
Scanner <object name>=new Scanner(System.in);
Inputting statements of different data types.
Integer:-
Scanner <object name>=new Scanner(System.in);
int <variable name>=<object name>.nextInt();
Double:-
Scanner <object name>=new Scanner(System.in);
double <variable name>=<object name>.nextDouble();
Short:-
Scanner <object name>=new Scanner(System.in);
short <variable name>=<object name>.nextShort();
Byte:-
Scanner <object name>=new Scanner(System.in);
byte <variable name>=<object name>.nextByte();
Long:-
Scanner <object name>=new Scanner(System.in);
long <variable name>=<object name>.nextLong();
Float:-
Scanner <object name>=new Scanner(System.in);
float <variable name>=<object name>.nextFloat();
Character:-
Scanner <object name>=new Scanner(System.in);
char <variable name>=<object name>.next().charAt(0);
String:-
There are two types of strings in scanner class:
- As a word
- As a text line
For word:
Scanner <object name>=new Scanner(System.in);
String <variable name>=<object name>.next();
Now, you can store characters or words but you can't store a sentence. Example you can store "8", "Rajat" etc
As a text line:-
For text line:-
Scanner <object name>=new Scanner(System.in);
String <variable name>=<object name>.nextLine();
Now, you can store all kinds of strings means characters ,sentences,words etc.
Example:-"I love cricket","Rajat","8".
Answer of the question:-
Syntax:-
Scanner in=new Scanner(System.in);
double n;
n=in.nextDouble();
in.close();
You can also use float but use double as double stores higher range of values as compared to float.
Syntax:-
Scanner in=new Scanner(System.in);
float n:
n=in.nextFloat();
in.close();