I need to split an input string into many parts. The splits should occur at "\n" (literally backslash-n, not the newline character). E.g., I want to turn this:
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw y #0000ff\ny = y & x\ndraw y #ff0000
into this:
x = [2,0,5,5]
y = [0,2,4,4]
draw y #0000ff
y = y & x
draw y #ff0000
I would have thought that stringArray = string.split("\n");
would have been sufficient.
But it gives me the same output as input in the following code:
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Input\n");
String s = br.readLine();
NewInterpreter interpreter = new NewInterpreter(s);
interpreter.run();
}
public NewInterpreter(String input) {
this.input = input;
this.index = 0;
this.inputComponents = input.split("\n");
System.out.println("Output: ");
for(String s : inputComponents)
System.out.println(s);
}
Enter Input
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw x #00ff00\ndraw y #0000ff\ny = y & x\ndraw y #ff0000"
Output:
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw x #00ff00\ndraw y #0000ff\ny = y & x\ndraw y #ff0000
Any help is greatly appreciated, thanks!