小编Mer*_*mas的帖子

在Java中拆分字符串,

我试图将一个字符串拆分成一个字符串数组,但是当它拆分字符串时,只有第一部分,在拆分之前,在[0]插槽中的数组中,但是[1]或更晚的内容中没有任何内容.这也会在尝试输出拼接时返回java异常错误[1]

import java.util.Scanner;

public class splittingString
{

    static String s;


    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the length and units with a space between them");
        s = input.next();
        String[] spliced = s.split("\\s+");
        System.out.println("You have entered " + spliced[0] + " in the units of" + spliced[1]);
    }

}
Run Code Online (Sandbox Code Playgroud)

java arrays string split java.util.scanner

2
推荐指数
2
解决办法
574
查看次数

标签 统计

arrays ×1

java ×1

java.util.scanner ×1

split ×1

string ×1