Computer Science, asked by 8bnaumyashukla, 2 months ago

Write down the syntax to accept a fractional value ‘n’ in double data type through Scanner class.​

Answers

Answered by anindyaadhikari13
6

\texttt{\textsf{\large{\underline{Answer}:}}}

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.

\texttt{\textsf{\large{\underline{Explanation}:}}}

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();

\texttt{\textsf{\large{\underline{More To Know}:}}}

Various method of Scanner class:

  1. nextInt(): Used to take integer as input.
  2. nextFloat(): Used to text floating type value as input.
  3. nextDouble(): Used to take double type value as input.
  4. nextLong(): Used to take a long type value as input.
  5. next(): Accepts the next token which is a string.
  6. nextLine(): Used to take a line of text as input.
Answered by kamalrajatjoshi94
1

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();

Similar questions