-3

I'm trying to figure out how to parse a specific input. The input looks like this: "[(x,y) (x,y) (x,y).......(x,y)]"

The "x" and "y" are integer values. Each go into variables called "x" and "y" respectively. Those values are then put into variables in a Node class. I then have an array of those nodes. However, I am struggling to figure out how to efficiently parse the input to get the x and y values. Thanks for your help.

EDIT:

I apologize for not providing any previous code. I had tried using a StringBuffer, but it became really messy, and I didn't know how to tell apart multidigit integers from single digit integers. I didn't mean to imply that anyone write code for me, I was simply asking if there were some various methods I could look up that anyone could recommend, so I apologize for that.

John Drumm
  • 25
  • 5
  • Regex is your friend ;) – user Apr 27 '15 at 21:33
  • Show us what you have tried... it's a little rude asking us to do all the work. – RudolphEst Apr 27 '15 at 21:40
  • You could remove two characters from both ends and split by `") ("`. Then parsing the `x,y` part is pretty obvious. Just an example, there are many ways. – Bubletan Apr 27 '15 at 21:43
  • It would be easier for us if you provided some very specific _code_ to discuss, including a demonstration of how it (doesn't) work, and what you've tried to fix it. – Kevin J. Chase Apr 27 '15 at 21:56

2 Answers2

1

The "most efficient" way is likely to be to hand-code the parser to step through the positions in the string etcetera. It is a lot of coding.

Fortunately, the simple (but less efficient) way is not too inefficient.

  • Use a regex (Pattern / Matcher) to match and extract the substring between the [ and ] characters.
  • Use String.split(...) to split between successive ) and ( characters.
  • Create the Node[] with the same number of entries as the previous split gave you.

  • In a loop:

    • Use a regex to match and extract the x and y numbers as strings.
    • Convert the strings to numbers
    • Build the Node object and add it to the array.

The rest is "just coding" (and reading javadocs), and I won't insult your intelligence by doing it for you1.


1 - @VoodooCoder's approach is equally valid ... apart from the implied insult to your intelligence :-)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

You can use Regex

public static void main(String [] args) {

    String input = "[(1,2) (3,4) (5,6)]";

    Pattern pattern = Pattern.compile("(\\d+),(\\d+)");
    Matcher matcher = pattern.matcher(input);

    List<Node> nodes = new ArrayList<Node>();
    Node node = null;
    while (matcher.find()) {
        node = new Node();
        node.setX(Integer.parseInt(matcher.group(1)));
        node.setY(Integer.parseInt(matcher.group(2)));
        nodes.add(node);
        System.out.printf("x=%d, y=%d\n", node.getX(), node.getY());
    }
}

Where Node is something like this:

public class Node {

    private int x;
    private int y;

    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

}

See more:

Community
  • 1
  • 1
antoniodvr
  • 1,259
  • 1
  • 14
  • 15