Hello i tried many ways to complete this but i failed. Could you help me ? I need double split one is "\\n" and second is "|". In textArea is string 350|450\\n 444|452\\n and so one. There are mouse coordinates. X|Y in int array i need
array[0]= x coord;
array[1]= y coord;
array[2]= x coord;
array[2]= y coord;
So i have string in textarea.I split that by "\\n"
String s[]= txtArea.getText().split("\\n");
This is one split and in textarea i have something like 150|255 this is my mouse coordinates x|y. So i need next split "|".
String s2[] = s[i].split("|");
After that
int [] array = new [s.length*2];
And some method for
while(!(s[j].equals("|")))
array[i] = Integer.parseInt(s[j]);
I tried something like that-
for(String line : txtArea.getText().split("\\n")){
arrayXY = line.split("\\|");
array = new int[arrayXY.length];
}
Thank you a lot for answers :) Have a nice day.
This can be solved easily using regex:
String[] split = input.split("\\||\n");
split will then contain the single numbers from the input.
Use Scanner
. It is always preferred over String.split()
.
Your code would then reduce to:
Scanner scanner = new Scanner(txtArea.getText());
scanner.useDelimiter("\\||\\n"); // i.e. | and the new line
for (int i = 0; i < array.length; i++) { // or use the structure you need
array[i] = scanner.nextInt();
}
Also, don't forget to import java.util.Scanner;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.