I have pattern/matcher lines that transform input Strings like this:
1 3 Hi [2 1 4]
into an Array like this:
[0] => "1"
[1] => "3"
[2] => "Hi"
[3] => "2 1 4"
That's the code:
String input = sc.nextLine();
Pattern p = Pattern.compile("(?<=\\[)[^\\]]+|\\w+");
Matcher m = p.matcher(input);
List<String> cIn = new ArrayList<String>();
while(m.find()) cIn.add(m.group());
Now I realised that sometimes I could get some negative values, inputs like 4 2 -1 2
. Since the input is a String, i can't really use any regular expression to get that negative value.
Below in the code, I use
Integer.parseInt(cIn.get(0));
to transform that string value into the Integer, that is actually what I need.
Could you figure a way that allows me to keep together the -
char and the number char? Then I would just check if there's the -
char to transform the number and multiply it by -1
. (If there's a better way I'd be glad to hear).
As usual, excuse me for my English.