當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.util.Scanner.hasNextShort()用法及代碼示例


描述

這個java.util.Scanner.hasNextShort()如果可以使用 nextShort() 方法將此掃描器輸入中的下一個標記解釋為默認基數中的短值,則方法返回 true。掃描儀不會通過任何輸入。

聲明

以下是聲明java.util.Scanner.hasNextShort()方法

public boolean hasNextShort()

參數

NA

返回值

當且僅當此掃描器的下一個標記是默認基數中的有效短值時,此方法才返回 true

異常

IllegalStateExceptionâˆ' 如果此掃描儀已關閉

示例

下麵的例子展示了 java.util.Scanner.hasNextShort() 方法的用法。

package com.tutorialspoint;

import java.util.*;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6 ";
      Short sh = 1;
      s = s + sh;

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // use US locale in order to be able to identify shorts in a string
      scanner.useLocale(Locale.US);

      while (scanner.hasNext()) {
         
         // check if the scanner's next token is a short
         System.out.println("" + scanner.hasNextShort());

         // print what is scanned
         System.out.println("" + scanner.next());
      }

      // close the scanner
      scanner.close();
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

false
Hello
false
World!
true
3
false
+
false
3.0
false
=
true
6
true
1

相關用法


注:本文由純淨天空篩選整理自 Java.util.Scanner.hasNextShort() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。