-2

Every time I try to execute this code it tells me that there is a lossy conversion from long to int. To my knowledge, there is not a single int in this code. The only thing I suspect is that one of the functions only returns an int. If so, how do I convert that into a long?

Also, does anyone see any errors in the last tow lines? I'm trying to convert an array into a list and I'm not sure that will work

And yes, I am aware that there are many other issues here, I'm just focused on this one specifically

import java.util.List;
class SumDigPower {
public static List<Long> sumDigPow(long a, long b) {
  long solution = 0;
  long solutionCounter = 0;
  long[] solutionArray;
  long[] digitArray;
  for (long i = a; i < b; i++) { 
    String strLong = Long.toString(i);
    String[] strArray = strLong.split("");
    for (long j = 0; j < strArray.length; j++) {
      digitArray[j] = Long.parseLong(strArray[j]);
    } for (long k = 0; k < digitArray.length; k++) {
      solution += Math.pow((k+1), digitArray[k]);
    } if (solution == i) {
      solutionArray[solutionCounter] = solution;
      solution = 0;
      solutionCounter += 1;
    }
  } List<Long> solutionList = Arrays.asList(solutionArray);
  return solutionList;
}
}
  • This code should not compile. The array index should be an int value and you use a long. For example : `digitArray[j] =` where `j` is declared as a `long`. – davidxxx Jul 01 '17 at 13:54
  • Your code has a lot of issues, the one you're asking about is easy enough - valid array indices are `int` (not `long`). – Elliott Frisch Jul 01 '17 at 13:54
  • Even when I change those indices to ints the problem still remains with solutionArray[solutionCounter] = solution; – Clayton Idestae Jul 01 '17 at 13:57

2 Answers2

1

Valid array indices are int (not long). You can't use an array without initializing it (and you need to know the length, because that's fixed with Java arrays). Next, you are wanting a List<Long> and trying to build an array (again, you can't do that - just use a List<Long> to begin with - and you can eliminate solutionCounter). Like,

public static List<Long> sumDigPow(long a, long b) {
    long solution = 0;
    List<Long> al = new ArrayList<>();
    for (long i = a; i < b; i++) {
        String strLong = Long.toString(i);
        String[] strArray = strLong.split("");
        long[] digitArray = new long[strArray.length];
        for (int j = 0; j < strArray.length; j++) {
            digitArray[j] = Long.parseLong(strArray[j]);
        }
        for (int k = 0; k < digitArray.length; k++) {
            solution += Math.pow((k + 1), digitArray[k]);
        }
        if (solution == i) {
            al.add(solution);
            solution = 0;
        }
    }
    return al;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You can't pick a value from from an array with a long. Try casting to int instead:

long myLong = 0;
int myInt = (int) i;

and then putting it as your index in an array:

myStringArray[myInt]