i want to split a multiple line input using split function when i tried it it was not working
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++;
}
}
}
input int a d g d ,
output ,
but the required output is int a d g d
You're splitting on str instead of 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++;
}
}
}