In this program I’m parsing input into integers and I can’t seem to be able to take in negative numbers and have tried a bunch of regular expressions. I can get positive integers no problem by removing the &&^- it’s just the negative integers I’m having problems with.
Example:
input = console.readLine("?> ").split("\\D+&&^-");
Any idea and what I’m doing wrong? Thanks for any help in advance.
Sample input:
-7 * 4
Output:
-7 and 4 should be stored into an array of strings.
&&is not an “and” operator inside a regular expression. I figure you meant to use[^0-9-]+, right?I recommend matching instead of splitting. However, neither will trivially solve the corner case of
-12-34for you. Splitting will give you a single, non-parseable number. Matching with-?\d+will give you two numbers, with nothing inbetween (you could treat that as a default+though).