VikingMoose VikingMoose - 1 year ago 64
Java Question

How to select particular values out of a string and turn them into an integer

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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download