扫描仪为String在Java中

问题描述:

Scanner x = new Scanner(web.openStream()); 
System.out.print(web.toString()); 

我想我的扫描仪的对象更改为一个字符串,但是当我打印出来,我得到这样的事情扫描仪为String在Java中

java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]/# 

这是为什么?

toString()仅表示字符串表示形式扫描器对象不是实际输入。如果你想读取数据使用next()函数如下

System.out.print(scanner.next()); 

你必须使用扫描仪本身。 根据你想要阅读的内容,它必须是x.next()或x.nextLine() 查看本教程。使用扫描仪时,只有教程的一部分对您很重要。 http://www.mkyong.com/java/how-to-read-input-from-console-java/

扫描仪类用于标记字符串。您在扫描器构造函数中作为参数提供的输入将作为分隔符。

如果要扫描的对象更改为字符串,并打印出来....我认为的代码将是:

Scanner x = new Scanner(web.openStream()); 
System.out.println(x.toString()); 

这将打印分隔字符串到控制台。

如果你要打印分隔符之后的字符串,则代码应该是:

Scanner x = new Scanner(web.openStream()); 
System.out.println(x.next());