php    php100   android
當前位置:首頁 » Java.util包 »

Java.util.Scanner.hasNextBoolean()方法實例

評論  編輯

描述

The java.util.Scanner.hasNextBoolean() method returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false". The scanner does not advance past the input that matched.

聲明

Following is the declaration for java.util.Scanner.hasNextBoolean() method

public boolean hasNextBoolean()

參數

  • NA

返回值

This method returns true if and only if this scanner's next token is a valid boolean value

異常

  • IllegalStateException -- if this scanner is closed

實例一

編輯 +分享實例

以下例子將告訴你如何使用 java.util.Scanner.hasNextBoolean() method.

package gitbook.net;

import java.util.*;

public class ScannerDemo {

   public static void main(String[] args) {

      String s = "Hello World! false 3 + 3.0 = 6";

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

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

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

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

編譯和執行以上程序,將得到以下的結果:

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

貢獻/合作者

正在開放中...
 

評論(條)

  • 還冇有評論!