1

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.

3
  • what exactly are you trying to do here ?? you need a result array which contains only the x and y coordinates ? Commented Sep 20, 2015 at 10:15
  • yes as i show in example arr[0] -x coord; arr[1] - y coord; Commented Sep 20, 2015 at 10:19
  • then @paul already gave you the best possible solution. Commented Sep 20, 2015 at 10:20

2 Answers 2

4

This can be solved easily using regex:

String[] split = input.split("\\||\n");

split will then contain the single numbers from the input.

Sign up to request clarification or add additional context in comments.

5 Comments

and after that how how do i know if number was one digit or more digit ?
if (number>9) then its a two digit number
no i mean mouse coord can be 0|356 or 22|0 or 456|478 how can i know it
pretty simple, since the numbers are still strings after the split: if(split[i].length() > 1)itsATwoDigitNumber();
Thank you Paul i am done with this and it is working Thank you a lot :) Have a nice day.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.