-3

Hi i'm having trouble saving my string.split value to an array my array is empty after i use the following code.

Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        in.close();
        if(input.matches("^[0-6][.][0-6]$"))
        {
            b = false;
            String[] coordinates = input.split(".");
            int c1 = Integer.parseInt(coordinates[0]);
            int c2 = Integer.parseInt(coordinates[1]);
            playingfield.PlayTurn(c1, c2);

it tells me coordinates[0] and [1] are null.

Everything up to that point works and the Regex is correct to As i can just print the String there and it works fine

Samuel
  • 65
  • 9

1 Answers1

5

Replace

String[] coordinates = input.split(".");

with

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

Split takes a regular expression as a parameter. . matches anything (meaning the whole String), not the dot character as you expect.

StephaneM
  • 4,779
  • 1
  • 16
  • 33