Программа для получения следующих выходных данных для данного входного сигнала
Eg 1: Input: a1b10 Output: abbbbbbbbbb Eg: 2: Input: b3c6d15 Output: bbbccccccddddddddddddddd The number varies from 1 to 99.
Что я уже пробовал:
static void printCharSeq(char c, int num) { while (num > 1) { System.out.print(c); num--; } } void convert(String str) { char c[] = str.toCharArray(); for (int i = 0; i < c.length; i++) { int val = 0; if ((c[i] >= '0' && c[i] <= '9')) { // val=c[i]-48; try { if (c[i + 1] >= '0' && c[i + 1] <= '9') { String a1 = String.valueOf(c[i] - 48); String a2 = String.valueOf(c[i + 1] - 48); val = Integer.parseInt(a1 + a2); // System.out.println(val); printCharSeq(c[i - 1], val); i++; } else { val = c[i] - 48; printCharSeq(c[i - 1], val); } } catch (ArrayIndexOutOfBoundsException ex) { val = c[i] - 48; printCharSeq(c[i - 1], val); } } else { System.out.print(c[i]); // printCharSeq(c[i], val); } } }
Here This logic works perfectly but when i try to input : v99t100cvrx2 Output : vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvttttttttttcvrxx it prints t 10 times too. i don't know ether it is correct or wrong. please help me to find it out the logic is correct for different inputs.