如何使用拆分函数拆分多行输入 java

how to split multiple line input using split function java

我想使用拆分功能拆分多行输入,但我试过它不起作用

    public static void main(String [] args)
{
    String TER = ",";
  int i=0;
    java.util.Scanner a = new java.util.Scanner(System.in);
    StringBuilder b = new StringBuilder();
    String str;
    while (!(str = a.nextLine()).equals(TER)) {
        b.append(str);//here i am getting the multiple line input
    }        
    String parts[] = str.split("\ ");
    while(i<parts.length)
    {
        System.out.println(parts[i]);
        i++;
    }
}

}

输入整数 a d g d ,

输出,

但所需的输出是 整数 一种 d G d

你在 str 而不是 b.toString()

上拆分
public class LoopTest {
    public static void main(String [] args) {
        String TER = ",";
        int i=0;
        java.util.Scanner a = new java.util.Scanner(System.in);
        StringBuilder b = new StringBuilder();
        String str;
        System.out.println("Enter a multiple line input"); //opens console window
        while (!(str = a.nextLine()).equals(TER)) {
            b.append(str);//here i am getting the multiple line input
        }
        System.out.println(b.toString());
        String parts[] = b.toString().split("\ ");
        while(i<parts.length) {
            System.out.println(parts[i]);
            i++;
        }
    }
}