Home » Java programming language

Java Scanner next() Method with Example

Scanner Class next() method: Here, we are going to learn about the next() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on March 24, 2020

Scanner Class next() method

Syntax:

    public String next();
    public String next(Pattern patt);
    public String next(String patt);
  • next() method is available in java.util package.
  • next() method is used to search & get the next complete token from this Scanner and a token is preceded & followed by the input that meets the pattern.
  • next(Pattern patt) method is used to retrieve the next token when it meets the given Pattern (patt).
  • next(String patt) method is used to retrieve the next token when it meets the pattern formed from the given String (patt).
  • These methods may throw an exception at the time of representing a token as a pattern.
    • NoSuchElementException: This exception may throw when no more token exists.
    • IllegalStateException: This exception may throw when this Scanner is not opened.
  • These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, next(),
    • It does not accept any parameter.
  • In the first case, next(Pattern patt),
    • Pattern patt – represents the pattern (patt) to read.
  • In the second case, next(String patt),
    • String patt – represents the string to define the pattern (patt) to read.

Return value:

In all the cases, the return type of the method is String, it retrieves the next token

Example 1:

// Java program is to demonstrate the example 
// of next() method of Scanner

import java.util.*;
import java.util.regex.*;

public class Next {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24";

  // Instantiates Scanner
  Scanner sc = new Scanner(str);

  // By using next() method is to
  // display the next complete 
  // token
  String next = sc.next();
  System.out.println("sc.next(): " + next);

  // Scanner closed
  sc.close();
 }
}

Output

sc.next(): Java

Example 2:

import java.util.*;
import java.util.regex.*;

public class Next {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24";

  // Instantiates Scanner
  Scanner sc = new Scanner(str);
  // By using net(Pattern) method is
  // to return the next token when it meets
  // the given pattern
  String next_p = sc.next(Pattern.compile("J..a"));
  System.out.println("sc.next(Pattern.compile(J..a)): " + next_p);

  // Scanner closed
  sc.close();
 }
}

Output

sc.next(Pattern.compile(J..a)): Java

Example 3:

import java.util.*;
import java.util.regex.*;

public class Next {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24";

  // Instantiates Scanner
  Scanner sc = new Scanner(str);

  // By using net(String) method is
  // to return the next token when it meets
  // the given pattern formed from the given
  // string
  String next_s = sc.next("Java");
  System.out.println("sc.next(Java)): " + next_s);

  // Scanner closed
  sc.close();
 }
}

Output

sc.next(Java)): Java



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.