Java Question

Error when I want to "int TAM_ARRAY = Nombre.length();"

So, I am new to all of this and I want to make a list of all my members in my class and it marks me an error, yes I am studing In Mexico so I get kind of confuse. Can anybody help me?

enter code here


package nombres;

import java.util.Arrays;
import java.util.Random;

public class Nombres {

public static void main(String[] args) {

String[] Lista;
String[] Nombre = {"Acosta Ochoa Sebastian", "Aguilar Gomez Jesus", "Alejandro Suarez Lidia", "Arevalo Alvarez Ezequiel", "Arias Varela Jair Eli", "Chacon Martinez Michael", "Cordova Contreras Gabriela Yajaira", "Cruz Dominguez Juan Carlos", "Delgado Rodriguez Daniel Raul", "Gallardo Echeverria Isaac Dali", "Gil Bautista Francisco Javier", "Gonzalez Hernandez Anabel", "Lopez Martinez Yesenia Eloisa", "Lopez Pablo Marcos Antonio", "Mendoza Rueda Saul", "Nadal Salaya Juan Carlos", "Ramirez Aguirre Jorge Luis ", "Ramos Ramos Adrian Adair", "Rodriguez Landero Alfredo", "Salaya Cruz Aaron", "Santos Meza Jonathan", "Solis Hernandez Marcel", "Solis Pereyra Alan", "Solis Pereyra Pedro Antonio", "Valencia Salaya Alexander", "Zapata Foster Aylin"};

int TAM_ARRAY = Nombre.length();

Random rand = new Random();

int posAleatoria = rand.nextInt(TAM_ARRAY - 25);

String str = Nombre[posAleatoria];

}

}

Answer

For a convention way of naming use nombre instead of Nombre as array name. Use length instead of length() method.

        String[] Lista;
        String[] nombre = {"Acosta Ochoa Sebastian", "Aguilar Gomez Jesus", "Alejandro Suarez Lidia", "Arevalo Alvarez Ezequiel", "Arias Varela Jair Eli", "Chacon Martinez Michael", "Cordova Contreras Gabriela Yajaira", "Cruz Dominguez Juan Carlos", "Delgado Rodriguez Daniel Raul", "Gallardo Echeverria Isaac Dali", "Gil Bautista Francisco Javier", "Gonzalez Hernandez Anabel", "Lopez Martinez Yesenia Eloisa", "Lopez Pablo Marcos Antonio", "Mendoza Rueda Saul", "Nadal Salaya Juan Carlos", "Ramirez Aguirre Jorge Luis ", "Ramos Ramos Adrian Adair", "Rodriguez Landero Alfredo", "Salaya Cruz Aaron", "Santos Meza Jonathan", "Solis Hernandez Marcel", "Solis Pereyra Alan", "Solis Pereyra Pedro Antonio", "Valencia Salaya Alexander", "Zapata Foster Aylin"};

        int TAM_ARRAY = nombre.length;

        Random rand = new Random();

        int posAleatoria = rand.nextInt(TAM_ARRAY)+1;//TAM_ARRAY is the maximum and the 1 is our minimum

        String str = nombre[posAleatoria];
        System.out.println(str);
Comments