Maybe someone find inspiration in my code. After almost seven hours non-stop working (not counting WC visits, and coffe/pizza break) I did this Frankenstain.
I start this task yesterday and I tried did it with Pattern and Matcher help. It's not work, and I ending on help section. I find beauty code from Evgen but I don't understand fully regex with (?=) questionmark, I could copy this and do the rest, but somewhere else I have read comment's that it's possible do this without it, use knowledge whose we have. It's unbelievable, I was in trance reading it, I couldn't do this one more time from scrach. I know that posting complete solutions is not allowed but I need more experienced eyes looking for my algorithm and tell me what I can do to simplify it! Because I write it for success, and I know my solutions is too long, and maybe not so neat and optimal.
while (reader.ready()) {
String line = reader.readLine();
StringBuilder sb = new StringBuilder();
char[] chars = line.toCharArray();
for (int i = 0; i < chars.length; i++) {
// is Number, different than 1, first element (2 edf) 2edf 2.r
if (Character.isDigit(chars[i]) && !String.valueOf(chars[i]).equals("1") && i == 0) {
if (i < chars.length - 1) {
// if next isn't number and alphabetic
if (!Character.isDigit(chars[i + 1]) && !Character.isAlphabetic(chars[i + 1])) {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
} else {
sb.append(chars[i]);
}
} else {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
}
// is Number 1 , first element (1 ede) 1we 123 12, 12
} else if (Character.isDigit(chars[i]) && String.valueOf(chars[i]).equals("1") && i == 0) {
if (i < chars.length - 1) {
if (!Character.isDigit(chars[i + 1]) && !Character.isAlphabetic(chars[i + 1])) {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
} else if (String.valueOf(chars[i + 1]).equals("0") || String.valueOf(chars[i + 1]).equals("1") || String.valueOf(chars[i + 1]).equals("2")) {
if (i + 1 < chars.length) {
if (!Character.isDigit(chars[i + 2]) && !Character.isAlphabetic(chars[i + 2])) {
String string = String.valueOf(chars[i]) + String.valueOf(chars[i + 1]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
i++;
} else {
sb.append(chars[i]).append(chars[i + 1]).append(chars[i + 2]);
i += 2;
}
} else {
String string = String.valueOf(chars[i]) + String.valueOf(chars[i + 1]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
i++;
}
}
} else {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
}
// if Number different than 1, last element 2 2ed2 2.2 fuck 2 12
} else if (Character.isDigit(chars[i]) && !String.valueOf(chars[i]).equals("1") && i == chars.length - 1) {
if (!Character.isDigit(chars[i - 1]) && !Character.isAlphabetic(chars[i - 1])) {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
} else {
sb.append(chars[i]);
}
// if Number == 1, last element 1 2ed1 2.1 fuck 1 11
} else if (Character.isDigit(chars[i]) && String.valueOf(chars[i]).equals("1") && i == chars.length - 1) {
if (!Character.isDigit(chars[i - 1]) && !Character.isAlphabetic(chars[i - 1])) {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
} else {
sb.append(chars[i]);
}
// is Number, different than 1, middle elements (2 edf) 2edf 2.r 12.
} else if (Character.isDigit(chars[i]) && !String.valueOf(chars[i]).equals("1")) {
if (!Character.isDigit(chars[i + 1]) && !Character.isAlphabetic(chars[i + 1]) &&
!Character.isDigit(chars[i - 1]) && !Character.isAlphabetic(chars[i - 1])) {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
} else {
sb.append(chars[i]);
}
// is Number 1 , middle elements (1 ede) 1we 123 12, 12.
} else if (Character.isDigit(chars[i]) && String.valueOf(chars[i]).equals("1")) {
if (!Character.isDigit(chars[i + 1]) && !Character.isAlphabetic(chars[i + 1]) &&
!Character.isDigit(chars[i - 1]) && !Character.isAlphabetic(chars[i - 1])) {
String string = String.valueOf(chars[i]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
} else if (String.valueOf(chars[i + 1]).equals("0") || String.valueOf(chars[i + 1]).equals("1") || String.valueOf(chars[i + 1]).equals("2")) {
if (i + 1 < chars.length) {
if (!Character.isDigit(chars[i + 2]) && !Character.isAlphabetic(chars[i + 2])) {
String string = String.valueOf(chars[i]) + String.valueOf(chars[i + 1]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
i++;
} else {
sb.append(chars[i]).append(chars[i + 1]).append(chars[i + 2]);
i += 2;
}
} else {
String string = String.valueOf(chars[i]) + String.valueOf(chars[i + 1]);
int data = Integer.parseInt(string);
sb.append(map.get(data));
i++;
}
} else {
sb.append(chars[i]);
}
} else {
sb.append(chars[i]);
}
if (debugMode()) {
System.out.println(sb);
}
}
System.out.println(sb.toString());
}