Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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 如何使用Scanner拆分以空格作为分隔符的字符串_Java_Java.util.scanner - Fatal编程技术网

Java 如何使用Scanner拆分以空格作为分隔符的字符串

Java 如何使用Scanner拆分以空格作为分隔符的字符串,java,java.util.scanner,Java,Java.util.scanner,我试图根据单词之间的空格来分割输入句子。它没有按预期工作 public static void main(String[] args) { Scanner scaninput=new Scanner(System.in); String inputSentence = scaninput.next(); String[] result=inputSentence.split("-"); // for(String iter:result) { //

我试图根据单词之间的空格来分割输入句子。它没有按预期工作

public static void main(String[] args) {
    Scanner scaninput=new Scanner(System.in);
    String inputSentence = scaninput.next();
    String[] result=inputSentence.split("-");
    // for(String iter:result) {
    //     System.out.println("iter:"+iter);
    // }
    System.out.println("result.length: "+result.length);
    for (int count=0;count<result.length;count++) {
        System.out.println("==");
        System.out.println(result[count]);
    }
}
当我用空格“”替换“-”时,它给出以下输出

first second third
result.length: 1
==
first
关于这里的问题有什么建议吗?我已经提到了stackoverflow post,但它不起作用

使用
split(\\s+”)
可获得以下输出:

first second third
result.length: 1
==
first

问题是,
scaninput.next()
将只读取到第一个空格字符,因此它只会将单词
first
拉入。因此,
split
之后将一事无成

我建议不要使用
Scanner
,而是使用
java.io.BufferedReader
,这样您就可以使用
Scanner
next()
方法在空格上拆分字符串,也就是说,它返回下一个标记,直到下一个字符串为止。因此,如果添加适当的
println
,您将看到
inputSequence
等于第一个单词,而不是整个字符串

scanInput.next()
替换为
scanInput.nextLine()
使用
src.split(\\s+)而不是
输入句子。拆分(“-”)

它在表示每个非空白字符的每个
\\s
上拆分。如果元素在该分隔符之前、之间和之后,则结果是数组

下面是您需要的完整示例

示例:

public class StringSplit {
    public static void main(String[] args) 
    {
        String src = "first second third";
        String[] stringArray = src.split("\\s+");

        System.out.println(stringArray[0]);
        System.out.println(stringArray[1]);
        System.out.println(stringArray[2]);
    }
}
有关拆分(“\s+”)工作原理的更多详细信息,请参阅blow stackoverflow post

改变

scanner.next()

扫描器使用定界符模式将其输入拆分为标记,默认情况下,定界符模式匹配空白

调用
next()
返回下一个单词。

调用
nextLine()
返回下一行

另一种选择是使用工作良好的缓冲读取器类

String inputSentence;

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            inputSentence=br.readLine();

            String[] result=inputSentence.split("\\s+");
rintln("result.length: "+result.length);

            for(int count=0;count<result.length;count++)
            {
                System.out.println("==");
                System.out.println(result[count]);
            }

        }
字符串输入语句;
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
InputSession=br.readLine();
字符串[]结果=InputSession.split(\\s+);
rintln(“result.length:+result.length”);

对于(int count=0;countTry使用
\\s+
如果有多个空格。告诉我们你到底尝试了什么。这是输出的第一秒第三个结果。length:1==第一次在我的问题中添加了相同的结果,以便更好地查看。无法在注释中格式化显示修改后的代码..将其放入程序中。整个代码..扫描仪没有错,他是t需要使用
nextLine()
或更换除沫器。哪一个更好?Scanner还是bufferedReader?
\s
是空白,而不是非空白
scanner.nextLine()
String inputSentence;

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            inputSentence=br.readLine();

            String[] result=inputSentence.split("\\s+");
rintln("result.length: "+result.length);

            for(int count=0;count<result.length;count++)
            {
                System.out.println("==");
                System.out.println(result[count]);
            }

        }