-1

I am working on strings and working on a problem. The problem statement is to "add one to all digits inside string".I am not getting desired output for input numbers 129 and 9923. can anyone please help!

import java.util.*;
public class Increment {
  public static void main(String[] args) {
    String number = "129";
    int len = number.length();
    int i = 0;
    int temp = 0;
    int before = 0;
    int carry = 0;

    String result = number;
    for (i = len - 1; i >= 0; i--) {
      temp = Integer.parseInt(number.charAt(i) + "");
      if (temp >= 0 && temp < 9) {
        carry = 0;
        temp = temp + 1;
        result = result.replace(number.charAt(i), (char)(temp + '0'));
      } else {
        carry = 1;
        if (i != 0) {
          before = Integer.parseInt(number.charAt(i - 1) + "");
          before = before + 1;
          result = result.replace(number.charAt(i), '0');
          result = result.replace(number.charAt(i - 1), (char)(before + carry));
          i = i - 1;
        } else {
          result = result.replace(number.charAt(i), '0');
          result = "1" + result;
        }
      }
    }
    System.out.println(result);
  }
}
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
  • 7
    You might want to tell us your desire output for those case. eG should `"129"` result in `"230"` or `"240"` or `"2310"` or something else? – OH GOD SPIDERS Jul 22 '22 at 13:52
  • 240 because for digit 9 you are adding 1 twice and 10034. – Koushik Andhavarapu Jul 22 '22 at 13:57
  • 2
    Since `129` should become `240` because 9+1=10 which means `1` will need to be added *again* to `2`, then why `9923` should become `10034` instead of `11034`? Try to think what `99` should become. When we increase right `9` it becomes `10`, which according to your logic should cause *another* addition for left 9 making it `110`. – Pshemo Jul 22 '22 at 14:01
  • @Pshemo I mean that for digit 9 it should be "add one normally and the carry". finally it should rounded to single digit. so (9+1+1 = 11) and replace by 1 and carry also now becomes 1. Thanks for correcting. – Koushik Andhavarapu Jul 22 '22 at 15:41
  • 2
    Please consider using [edit] option to add all necessary information to the question itself, like expected result and logic behind it. Lets not force people who may be willing to help you to also search for all necessary information all over comment section. – Pshemo Jul 22 '22 at 16:00

3 Answers3

0

You define a method for adding two strings and call that method

public static String addStrings(String num1, String num2) {
  StringBuilder sb = new StringBuilder();

  int i = num1.length() - 1, j = num2.length() - 1;
  int carry = 0, sum = 0;
  while (i >= 0 || j >= 0) {
    sum = carry;

    if (i >= 0) sum += num1.charAt(i) - '0';
    if (j >= 0) sum += num2.charAt(j) - '0';

    sb.append(sum % 10);
    carry = sum / 10;
    i--;
    j--;
  }

  if (carry != 0) sb.append(carry);

  return sb.reverse().toString();
}

, main

public static void main(String[] args) {
  String num1 = "129";
  String num2 = "9923";
  String res1 = addStrings(num1, "1".repeat(num1.length()));
  String res2 = addStrings(num2, "1".repeat(num2.length()));

  System.out.println(res1);
  System.out.println(res2);
}

, output

240
11034
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
0

I would use regex, it makes it a much simpler solution:

public static void main(String[] args) {
    String text = "text240 moretext 350 evenmore460text";
    Pattern pattern = Pattern.compile("\\d+");

    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String value = matcher.group();
        int val = Integer.parseInt(value) + 1;
        text = text.replace(value, Integer.toString(val));
    }
    System.out.println(text);
}
Ryan
  • 1,762
  • 6
  • 11
  • Fair enough. But this isn't really means as a 100% solution, just an example of another way to do it. – Ryan Jul 22 '22 at 19:03
0

The problem is this line of code, check here for more info

result = result.replace(number.charAt(i - 1), (char) (before + carry));

You may change it like below, but that would replace all occurrences of first argument as @user16320675 points out

result = result.replace(number.charAt(i - 1), Character.forDigit(before + carry, 10));

So, I would suggest to use StringBuilder instead of String in order to take advantage of the setCharAt(int idx, char c) method

AddeusExMachina
  • 592
  • 1
  • 11
  • 22
  • 2
    I *believe* that `replace` will replace **all** occurrences of first argument, so that will still mess up the result (if we have repeated digits and single carry) – user16320675 Jul 22 '22 at 14:06
  • Now I get it, with replace all ocurrences of 9 will change to 0 and that is why it work in case of 9923 and it does not go into if statement for adding one to the start. – Koushik Andhavarapu Jul 22 '22 at 16:31