Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

-The Scanner methods next(), nextInt(), and nextDouble() retrieve the next token

ID: 3702185 • Letter: #

Question

-The Scanner methods next(), nextInt(), and nextDouble() retrieve the next token in the buffer up to the next whitespace, which might be a space, tab, or Enter key.
-The nextLine() method reads all data up to the Enter key character.
The nextLine() method does not retrieve data until the user presses Enter;the other methods retrieve data when the user presses Enter, the spacebar, or the tab key.
i dont understand these sentenses above in java   
?and next() static or not static and why?
nextInt(), static or not static and why?
?nextdouble(),static or not static and why?
nextline()static or not static and why??

?and what is the nextline()?
?what is the next()?
what is difference between nextline()? and next() in java?
and i want simple code in java explain it the methods next() and nextline()?


Explanation / Answer

next() : Finds and returns the next complete token from this scanner.

nextInt() : Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input.

nextDouble() :  Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. The scanner does not advance past any input.

Note :The Scanner methods next(), nextInt(), and nextDouble() retrieve the next token in the buffer up to the next whitespace, which might be a space, tab, or Enter key.

All these methods nextLine(),next(),nextInt(),nextDouble() all are Scanner instance methods ,but not static .

Example Program :

//ScannerDemo2.java

// Java program to read some values using Scanner

// class and print their mean.

import java.util.Scanner;

public class ScannerDemo2

{

public static void main(String[] args)

{

// Declare an object and initialize with

// predefined standard input object

Scanner sc = new Scanner(System.in);

// Initialize sum and count of input elements

String str,str1;

int i = 0, count = 0;

Double j;

//Reading and display values

System.out.println("Enter any token " );

str=sc.next();

System.out.println("Enter token value is : "+ str);

System.out.println("Enter any integer value " );

i=sc.nextInt();

System.out.println("Enter integer value is : "+ i);

System.out.println("Enter any Double value " );

j=sc.nextDouble();

System.out.println("Enter Double value is : "+ j);

System.out.println("Enter any line with spaces untill enter press " );

str1=sc.nextLine();

System.out.println("Enter line is : "+ str1);

}

}