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

贡献/合作者

正在开放中...
 

评论(条)

  • 还没有评论!