1

I'm trying to make something that gives us the multiplicative root and the multiplicative persistence of a number.
So I made a method that gives us a list with 2 elements- the root and the persistence. So I made an empty list named answers and then added the elements in an if and while loop.
But when I then tried to print the List it gives an error.
Here's the code I made:

import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        long number = 123456789;
        String dupeNumber=String.valueOf(number);
        List<Long> answers;
        if (dupeNumber.length() > 1) {
            while (dupeNumber.length() > 1) {
                long intNumber = Integer.parseInt(dupeNumber);
                List<Long> digits = separated(intNumber);
                answers = prodDigits(digits);
                dupeNumber=String.valueOf(answers);
                System.out.println(dupeNumber);
            }
        }
        System.out.println(answers);
    }
    static List<Long> separated(long number) {
        List<Long> digits = new ArrayList<>();
        while (number > 0) {
            digits.add((number % 10));
            number -= (number % 10);
            number /= 10;
        }
        return digits;
    }
    static List<Long> prodDigits(List<Long> digits) {
        List<Long> answers = new ArrayList<>();
        long product = 1;
        long persistence = 0;
        for (long i : digits) {
            product *= i;
            persistence += 1;
        }
        answers.add(product);
        answers.add(persistence);
        return answers;
    }
}

The error and the error line is

- 18:28 java: variable answers might not have been initialized  

I want the output to be like this:
If the number given is 123456789: [0, 2] as 123456789=362800 and 36280*0=0 and multiplication happened 2 times.

vszholobov
  • 2,133
  • 1
  • 8
  • 23
Gon49
  • 17
  • 6

1 Answers1

1

In your code, you don't necessarily initialize the answers variable (from the compiler's point of view) and try to access it. Add a start value to it:

List<Long> answers = null; // or some default value, like new ArrayList<>()

How to fix "variable might not have been initialized" error in Java?

Update:

Instead of counting multiplicative persistence you count number of digits in number:

for (long i : digits) {
    product *= i;
    persistence += 1;
}

Insted you need to count number of calls of prodDigits() before number will stop changing.

Multiplicative Persistence

Final code may look like:

public static void main(String[] args) {
    long number = 123456789;

    int persistence = 0;

    while(number > 9) {
        number = prodDigits(separated(number));
        persistence++;
    }

    System.out.println(number + " " + persistence);
}

static List<Long> separated(long number) {
    List<Long> digits = new ArrayList<>();
    while(number > 0) {
        digits.add((number % 10));
        number -= (number % 10);
        number /= 10;
    }
    return digits;
}

static long prodDigits(List<Long> digits) {
    long product = 1;

    for(long i : digits) {
        product *= i;
    }

    return product;
}

We call prodDigits() until number consists of at least two digits. Also we count number of calls of prodDigits to find multiplicative persistence. At the end of loop number variable will be a multiplicative digital root.

Output:

0 2

Also you could do it a bit easier:

public static void main(String[] args) {
    long number = 123456789;

    System.out.println(persistenceAndRoot(number));
}

public static List<Long> persistenceAndRoot(long num) {
    long numberOfTimes = 0;
    long temp = num;

    while (num > 9) {
        temp = 1;
        while (num > 0) {
            temp *= num % 10;
            num /= 10;
        }
        num = temp;
        numberOfTimes++;
    }

    return List.of(temp, numberOfTimes);
}

Output:

[0, 2]
vszholobov
  • 2,133
  • 1
  • 8
  • 23
  • I tried to make it to null but it gives another error. Exception in thread "main" java.lang.NumberFormatException: For input string: "[362880, 9]" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:660) at java.base/java.lang.Integer.parseInt(Integer.java:778) at com.company.Main.main(Main.java:11) – Gon49 Aug 06 '21 at 03:29
  • Also, this error occurs after giving the output , ie. [362880, 9], even though it should have given the output [0, 2] – Gon49 Aug 06 '21 at 03:32
  • It happens, because you are trying to call `parseInt` method on string, which is not a number `"[362880, 9]"`. – vszholobov Aug 06 '21 at 03:32
  • So is there any way to solve the problem? – Gon49 Aug 06 '21 at 03:35
  • But it is not giving the correct output as the first number should be 0 as the multiplication needs to be carried until the number is of 1 digit – Gon49 Aug 06 '21 at 04:11
  • Updated answer. – vszholobov Aug 06 '21 at 04:21
  • It did. Thanks. – Gon49 Aug 08 '21 at 11:55