I have a String like the following 2 4 12 12 yellow Hi how are you
I want to split the string like this {2,2,12,12,yellow, Hi how are you}
in order to pass the items in the list as parameters in a constructor.
Any help?
I have a String like the following 2 4 12 12 yellow Hi how are you
I want to split the string like this {2,2,12,12,yellow, Hi how are you}
in order to pass the items in the list as parameters in a constructor.
Any help?
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.