Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用hasNext()函数_Java_Function_Token - Fatal编程技术网

如何在java中使用hasNext()函数

如何在java中使用hasNext()函数,java,function,token,Java,Function,Token,我是Java的新手,正在学习基本的步骤。我在CoreJava教科书和这个在线网站上找到了这个文档: 作者提到了一些关于token的东西,我无法理解。而且,我仍然觉得要弄清楚这个函数的主要作用还很模糊。 以下是该网站的代码: package com.tutorialspoint; import java.util.*; import java.util.regex.Pattern; public class ScannerDemo { public static void main

我是Java的新手,正在学习基本的步骤。我在CoreJava教科书和这个在线网站上找到了这个文档: 作者提到了一些关于token的东西,我无法理解。而且,我仍然觉得要弄清楚这个函数的主要作用还很模糊。 以下是该网站的代码:

package com.tutorialspoint;    
import java.util.*;
import java.util.regex.Pattern;

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

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

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

      // check if the scanner has a token
      System.out.println("" + scanner.hasNext());

      // print the rest of the string
      System.out.println("" + scanner.nextLine());

      // check if the scanner has a token after printing the line
      System.out.println("" + scanner.hasNext());

      // close the scanner
      scanner.close();
   }
}
我不明白为什么它和第一行不同

System.out.println(“+scanner.hasNext())

第二,但结果不同(真到假)

hasNext()
函数告诉我们:您有一个令牌(在您的例子中,您有一个字符串)

当您第一次调用
hasNext()
时,它将返回true(因为您在
扫描器中链接了字符串s
对象)


当您使用
nextLine
时,您在输出中表示字符串s,这样scanner对象就没有其他标记(字符串),因此在第二次调用时
hasNext()
返回false。

scanner
将处理输入。在这种情况下,检查输入字符串中是否有要读取的内容。因为你没有处理任何事情,所以答案是正确的。 使用
nextLine()
使用输入后,没有任何内容可供扫描仪消费者处理然后,
hasNext()
回答为false。

标记就是使用分隔符模式分割的扫描仪输入(我相信默认为空白)

当有输入时,您第一次将
true
作为输出(即
hasNext()
正在执行tin上的操作,它有输入)

然后使用
nextLine()

当您再次尝试调用
hasNext()
时,它是false,因为基本上您已经在print语句中使用了一行文本(在本例中只有一行),因此没有更多的输入,因此
hasNext()
是false

从文档中:

hasNext()

如果此扫描仪的输入中有另一个令牌,则返回true

hasNextLine()

如果此扫描仪的输入中有另一行,则返回true


nextLine
使用了它拥有的
next
。我了解nextLine的用法,但还不知道如何使用hasNext()及其函数。步骤1:停止使用tutorialspoint。寻找那些在语言基础上没有犯错误的人所写的资料。@Stultuske+1 tutorialPoint在很大程度上混淆了matterboth行的意思。检查是否有要使用的线路。就在消耗线路之前和消耗线路之后。