Home » Java programming language

Java Scanner nextShort() Method with Example

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

Scanner Class nextShort() method

Syntax:

    public short nextShort();
    public short nextShort(int rad);
  • nextShort() method is available in java.util package.
  • nextShort() method is used to read the next token of the input as a short value at the implicit radix of this Scanner.
  • nextShort(int rad) method is used to read the next token of the input as a short value at the explicit or given radix(rad) of this Scanner.
  • These methods may throw an exception at the time of representing an input as a short value.
    • InputMismatchException: This exception may throw when the next token of the input mismatch.
    • NoSuchElementException: This exception may throw when no such element exists.
    • IllegalStateException: This exception may throw when this Scanner is not opened.
  • These are non-static methods and it is accessible with the class object only and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, nextShort(),
    • It does not accept any parameter.
  • In the second case, nextShort(int rad),
    • int rad – represents the radix used to manipulate the token as a short value.

Return value:

In both the cases, the return type of the method is short, it retrieves the short value read from the input.

Example 1:

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

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

public class NextShort {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24 + sh";
  short sh = 1012;

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

  while (sc.hasNext()) {
   // By using nextShort() method isto
   // return the next token as a 
   // short at the implicit radix
   if (sc.hasNextShort()) {
    short next_s = sc.nextShort();
    System.out.println("sc.nextShort()): " + next_s);
   }
   System.out.println(sc.next());
  }

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

Output

Java
Programming!
sc.nextShort()): 3
*
8=
sc.nextShort()): 24
+
sh

Example 2:

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

public class NextShort {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24 + sh";
  short sh = 1012;

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

  while (sc.hasNext()) {
   // By using nextShort(9) method isto
   // return the next token as a 
   // short at the explicit radix
   if (sc.hasNextShort()) {
    short next_s = sc.nextShort(9);
    System.out.println("sc.nextShort(9)): " + next_s);
   }
   System.out.println(sc.next());
  }

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

Output

Java
Programming!
sc.nextShort(9)): 3
*
8=
sc.nextShort(9)): 22
+
sh


Comments and Discussions!

Load comments ↻





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