Taking Java Input: Using different types of next() methods according to Data Types
To take inputs in Java, we use next()
method. But next()
only takes String
input until a space occurs. So in this blog we will learn the in details about inputs in Java.
First of all we need to import Java's util
package to use the Scanner
class that have next()
method in it. To do that we need to write below code:
import java.util.*; // importing all classes and interfaces from util package
Otherwise, we can only import Scanner
class too:
import java.util.Scanner // importing only Scanner Class
The next()
method is a part of the java.util.Scanner
class in Java. This method finds and returns the next complete token from the scanner which is in use. A token is recognized as a complete entity in string form, such as a word.
Using Scanner
class we will then create a object of any name. Lets take sc
as the object name →
Scanner sc = new Scanner(System.in);
Now object sc
will finally let us use the next()
method to catch inputs from uses written in Terminal. Before we do that, we also need to store the input some where to work later. As next()
only takes String values, we will store it in a String variable (lets name that variable str
) →
String str= sc.next();
Full Code & Syntax
The syntax of the next()
method is:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Input: ");
String str= sc.next();
System.out.println("User's Input: " + str);
}
}
Here is output →
Functionality
When you call the next()
method, it scans the input for the next token, skipping over any delimiters (which are whitespace by default). Once it finds a token, it returns that token. If no more tokens are available, it throws a NoSuchElementException
.
In this code, the next()
method waits for the user to input a string and then assigns that string to the str
variable.
Different Data Types & next() methods
While the next()
method is useful, it’s important to note that it only returns the next token. If the user inputs multiple words, next()
will only return the first word. To get a full line of input, you should use the nextLine()
method instead. Look The Example Below →
As a solution Java has different methods for taking different Data Types →
nextInt()
: This method reads anint
value from the user.
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
int data = input.nextInt();
System.out.println("Using nextInt(): " + data);
2. nextDouble()
: This method reads a double
value from the user.
Scanner input = new Scanner(System.in);
System.out.print("Enter Double value: ");
double value = input.nextDouble();
System.out.println("Using nextDouble(): " + value);
3. nextLine()
: This method reads a line of text from the user.
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.println("My name is " + name);
4. next()
: This method reads a word from the user.
5. nextFloat()
: This method reads a float
value from the user.
6. nextBoolean()
: This method reads a boolean
value from the user.
7. nextByte()
: This method reads a byte
value from the user.
8. nextShort()
: This method reads a short
value from the user.
9. nextLong()
: This method reads a long
value from the user.
These all available methods from Scanner
class are used to capture different data types as mentioned above.
Conclusion
The next()
method other similar methods in Java’s Scanner
class are ways for parsing input as we need in different use cases. Whether you’re reading user input or parsing a file, understanding how to use next()
will be beneficial.