VikingMoose - 1 month ago 9

Java Question

I'm using a scanner to obtain a string containing an X and Y coordinate in the form of X,Y this is then saved as String XY1, how do i get the numbers of X and Y so that i can make int X1 equal to X and int Y1 equal to Y also ignoring the ","

Answer

If you're sure there's a comma inside the string, and the 2 parts look like integers :

```
String xy1 = "123,4567";
String[] integers = xy1.split(",");
int x1 = Integer.valueOf(integers[0]);
int y1 = Integer.valueOf(integers[1]);
System.out.println(x1 + " " + y1);
```

It outputs

```
123 4567
```

Source (Stackoverflow)

Comments