[Solved] Split a string using white space java [duplicate]


The trivial answer is to split the string:

String[] fragments = theString.split(" ", 6);

Given that the fragments have specific meanings (and presumably you want some of them as non-string types), it might be more readable to use a Scanner:

Scanner sc = new Scanner(theString);
int x = sc.nextInt();
int y = sc.nextInt();
int width = sc.nextInt();
int height = sc.nextInt();
String color = sc.next();
String message = sc.nextLine();

This approach is also easier if you are reading these strings, say, from a file or standard input: just create the Scanner over the FileReader/InputStream instead.

solved Split a string using white space java [duplicate]