if a character is a digit between 0 and 8 then increase it by 1 (others characters are unchanged)

Viewed 136
public String f2(String string) {
    String result = "";
    String num = "";
    for (int i = 0; i < string.length(); i++) {
            if (Character.isDigit(string.charAt(i))) {
                int number = Integer.parseInt(string.charAt(i) + "");
                if (number > 0 || number < 8) {
                    number += 1;
                    num = String.valueOf(number);
                }
            }
        }
    return result;
}

Example of input:

123abc

Expected output:

234abc

I solved the increase in the value of the character by 1 unit if it was a number, but I haven't solved the problem of putting the characters together.

Can someone help me?

5 Answers

First, a general note about your solution - chars can be comapred directly with the < and > operators, as well as incremented, so you don't need to go through the conversion of a specific character to an int.

With regard to collecting the modified characters, yhe easiest approach would probably be to use a StringBuilder:

public static String f2(String string) {
    StringBuilder sb = new StringBuilder(string.length());
    for (int i = 0; i < string.length(); ++i) {
        char ch = string.charAt(i);
        if (ch >= '0' && ch <= '8') {
            ch++;
        }
        sb.append(ch);
    }
    return sb.toString();
}

Using your code, I have arrived to that solution:

public String f2(String string) {
    StringBuilder result = new StringBuilder(string.length());
    for (int i = 0; i < string.length(); i++) {
        char c = string.charAt(i);
        if (c >= '0' && c <= '8') {
            result.append(++c);
        } else {
            result.append(string.charAt(i));
        }
    }
    return result.toString();
}

As you can see I check whether is a valid character (in your case a number between 0-8), and if it is I append the successor of it to result, in case not I just simply append it to result. So now it works for 123abc case and 1e3f4t5z case too.

You have missed appending the values to result. You need to append the incremented digit or the character as it is (if it is not in the range of 0 - 8) to result as shown below:

Do it as follows:

public class Main {
    public static void main(String[] args) {
        System.out.println(f2("123abc"));

    }

    public static String f2(String string) {
        String result = "";
        String num = "";
        for (int i = 0; i < string.length(); i++) {
            if (Character.isDigit(string.charAt(i))) {
                int number = Integer.parseInt(string.charAt(i) + "");
                if (number > 0 || number < 8) {
                    number += 1;
                    num = String.valueOf(number);
                    result += num;// Add it here
                }
            } else {// Add it here
                result += string.charAt(i);
            }
        }
        return result;
    }
}

Output:

234abc

Using Java 8 (or above), you can do like below:

public String f2(String string) {

    return string.codePoints()
            .map(ch -> (ch >= '0' && ch <= '8') ? ch + 1 : ch)
            .collect(StringBuilder::new,
                     StringBuilder::appendCodePoint,
                     StringBuilder::append)
            .toString();

}

Digits from 0 to 8 in the UTF-8 characters table have decimal codes from 48 to 56. Each character in Java has its own decimal code. The String class has the old replace method, which replaces all occurrences of a single character with another character. The simplest way here is to use a reverse loop:

String string = "abcdefghi.. 0123456789 003";
String string2 = string;

for (int i = 56; i > 47; i--) {
    string2 = string2.replace((char) i, (char) (i + 1));
}

System.out.println(string);  // abcdefghi.. 0123456789 003
System.out.println(string2); // abcdefghi.. 1234567899 114
Related