Problem getting random integer values in array

3

Eclipse does not point to any errors in the code, but does not execute.

package gerarOrganizar;
import java.util.Random;
public class GerarOrganizar {
    public static void main(String[] args) {
        int i=0;
        int[]ranF =null;
        ranF=new int[100];
        int ran=0;

        for(i=0;i<=100;i++){
            ran=new Random().nextInt(250);
            ranF[i]=ran;
            System.out.print(ran);//mandei imprimir apenas para saber se os valores 
                                  //estão sendo gerados, mas também não imprimiu.
        }//for
    }//main    
}//class
    
asked by anonymous 08.10.2014 / 19:10

3 answers

5

Eclipse does not point to any errors since you are not experiencing a syntax problem, but a logic problem. Your code even runs, but is stopped during execution due to an exception thrown.

Your for is using i as a counter, and is going from 0 to 100, in total, it will execute loop by 101 times. However, its ranF array only supports 100 positions, when the loop runs for the 101st time an exception will be thrown because you are trying to write a position beyond the last one. Change your for so that it runs 100 times instead of 101, like this:

for(i=0;i<100;i++){

Note that System.out.print() has everything printed on the same line as your console, to make it more readable you can break the line or concatenate a space between each printed number. Example:

System.out.println(ran);

or:

System.out.print(ran + " ");
    
08.10.2014 / 19:15
5

I found the code very confusing and rewrote it to try to understand and it worked like this:

package gerarOrganizar;
import java.util.Random;
public class GerarOrganizar {
    public static void main(String[] args) {
        int[] numerosAleatorios = new int[100];
        Random geradorRandomico = new Random();
        for(int i = 0; i < 100; i++) {
            numerosAleatorios[i] = geradorRandomico.nextInt(250);
            System.out.println(numerosAleatorios[i]); //deixei para você ver funcionando
        }
    }
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
08.10.2014 / 19:47
0

In the existing responses, I felt lack of cohesion in the code.

From the disambiguation page of the Cohesion , on Wikipedia in English , we can see that cohesion is a measure of relationship between parties. There's even an entry on code cohesion:

  

Cohesion (computer science) , a measure of how well the lines of source code within a module work together

In free translation (emphasis added):

  

Cohesion (computing science), a measure of how well the source code lines inside a module work together

In the examples, it was not for the cohesion of the code, but for its coherence. There was no strong relationship between the size of the vector and the amount of iteration elements. If there were any changes in the vector size, to maintain consistency, it would also be necessary to edit the iteration limit. This is due to the lack of cohesion between the size of the vector and the iteration.

To give more cohesion to the code, we have some possibilities. One of them is by a constant size of the vector, calling it whenever it is needed. But my favorite is to ask the vector itself what its size is, through numerosAleatorios.length , an attribute that gives the size of the vector in question. More details in this question .

I have adapted the @Maniero's code to exemplify cohesion;

package gerarOrganizar;
import java.util.Random;
public class GerarOrganizar {
    public static void main(String[] args) {
        int[] numerosAleatorios = new int[100];
        Random geradorRandomico = new Random();
        for(int i = 0; i < numerosAleatorios.length; i++) {
            numerosAleatorios[i] = geradorRandomico.nextInt(250);
            System.out.println(numerosAleatorios[i]); //deixei para você ver funcionando
        }
    }
}

Increasing code cohesion is considered a good practice , because when used correctly it will help maintain code at a later time. You are not required to use it, and it can also be negative if you want to increase the cohesion of a code just to make it more cohesive, without making it easier for you to maintain it.

You may well have an extremely non-cohesive but functional and coherent code, but this usually implies being extremely disciplined (the higher the code, the more discipline you need to keep it consistent).

    
19.10.2017 / 18:09