2

I'm making a histogram program in which I have one array that generates a random number, another that makes them into an array, and a last one that attempts to use that array and tell how many characters are in each [] of the array. My problem is that I cannot find a way to count how many characters are in each array element and outprint it. I'm trying to use the .length function but it doesn't seem to be working. Is there another way that I could do this? Here is my code. My problem is with my last method, before my main method.

package arrayhistogram;

/**
 *
 * @author Dominique
 */
public class ArrayHistogram {

   public static int randomInt(int low, int high){ 
double x =Math.random ()* (high - low)+low;
  int x1=(int)x;

   return x1;
}
   public static int[] randomIntArray(int n){
       int[] a = new int[n];
 for (int i=0; i<n; i++) {
 a[i] = randomInt(-5, 15);
 }
 return a;
   }
   public static int[] arrayHist () {
int[] x=randomIntArray(30);
       for (int i = 0; i < x.length; i++) { 

System.out.println(x[i].length);
}

return x;     
}

    public static void main(String[] args) {
       arrayHist();


    }

}

2 Answers2

0

There is no length property on an int. You can use this to get the number of digits in the int.

String.valueOf(x[i]).length()

This first transforms the int into a String and then returns the length of that String. E.g 123 => "123", whose length is 3.

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
0

Replace

System.out.println(x[i].length);

with

System.out.println(Integer.toString(x[i]).length());
Yev
  • 321
  • 3
  • 9