-1

I am trying to update event bus function which also updates progress bar. But it is not updating. I used BACKGROUND, ASYNC, MAIN Threads in EVENT BUS but still whenever I update the page progress bar will change and its value is updated but i can not do it without updating the whole page.

@Subscribe(threadMode = ThreadMode.BACKGROUND)
fun onPercentageEvent(percent: DownloadEvent){
    adapter.setOnChangeProgressListener{ id, view ->
        var progress = view as ProgressBar
        progress.progress = percent.percent.toInt()
        Timber.tag("SSS").d("progress: ${percent.percent}")
    }
}

It is the place where I am using EVENT BUS to update my progress bar

  • 1
    Solve it like you're doing it on paper, create a loop that would add the first(right most) 2 digits taking note if it's greater than 10(carry). work your way to the left most digit. Also store everything as string, except of course the 2 digits you're adding inside the loop – crimson589 Dec 07 '19 at 06:01
  • Please read this: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – Stephen C Dec 07 '19 at 06:17
  • Its a basic thing that we get to learn in school. you just have to code it. – Ashutosh Dec 07 '19 at 06:21
  • https://stackoverflow.com/questions/5318068/how-to-handle-very-large-numbers-in-java-without-using-java-math-biginteger Possible Repeat? – Sourish Mukherjee Dec 07 '19 at 06:21
  • Hi Jakhongir Jalilov. Please create a new question if you want to ask something fundamentally different than what you got an answer for; even if indirectly via a duplicate, but especially if people actually wrote an individual answer for you. – Yunnosch Nov 16 '20 at 08:35

2 Answers2

1

You can write something like BigInteger on your own if that's your assignment, but readability and performance will be relatively poor:

String num1 = "216725";
String num2 =  "72681"; // two numbers
StringBuilder result = new StringBuilder();
// reverse to make iterating easier
String reversedNum1= new StringBuilder(num1).reverse().toString(), reversedNum2 = new StringBuilder(num2).reverse().toString();
// keeping track if previous index sum was at least 10
int carry = 0;
for (int i = 0; i < Math.max(num1.length(), num2.length()); i++) {
    int sum = carry;
    if (num1.length() > i) sum += Integer.parseInt(reversedNum1.substring(i, i + 1));
    if (num2.length() > i) sum += Integer.parseInt(reversedNum2.substring(i, i + 1));
    if (sum >= 10) {
        sum -= 10;
        carry = 1; // 1 needs to be added in another iteration
    } else {
        carry = 0;
    }
    result.append(sum);
}
if (carry == 1) result.append(1);
System.out.println(result.reverse().toString());

The same way you would do when using a sheet of paper and a pen. Going from the end (that's why reversing), adding numbers on the same indices (if it's at least 10, adding 1 to another pair).

user1729
  • 95
  • 1
  • 7
0

after doing on a paper I written this and that works

public static void main(String[] args) {
    String input1 = "321654215";
    String input2 = "789456123";
    int[] arr1 = char2int(input1.toCharArray());
    int[] arr2 = char2int(input2.toCharArray());
    int[] intAdded = add(arr1, arr2);
    String resultat = new String(int2char(intAdded));
    System.out.println("  " + input1);
    System.out.println("+ " + input2);
    System.out.println("_____________");
    System.out.println(" " + resultat);
}

public static char[] int2char(int[] tab) {
    char[] res = new char[tab.length];
    for (int i = 0; i < tab.length; i++) {
        res[i] = (char)(tab[i] + 48);
    }
    return res;
}


public static int[] char2int(char[] tab) {
    int[] res = new int[tab.length];
    for (int i = 0; i < tab.length; i++) {
        res[i] = (int)tab[i] - 48;
    }
    return res;
}

public static int[] add (int[] tab1, int[] tab2) {
    int[] res = new int[Math.max(tab1.length, tab2.length) + 1];
    int carry = 0;
    for (int i = res.length - 1; i > 0 ; i--) {
        int sum = tab1[i-1] + tab2[i-1] + carry;;
        if (sum < 10) {
            res[i] = sum;
            carry = 0;
        }
        else {
            carry = 1;
            res[i]  = sum - 10;
        }
    }
    res[0] = carry;
    return res;
}

output :

  321654215
+ 789456123
_____________
 1111110338

I've transformed each string into a array of Stream and systematicaly add the carry of the level i to opérandes of the level i+1

kevin ternet
  • 4,514
  • 2
  • 19
  • 27