Retornar array de um método

3 respostas
G

Galera, estou com o seguinte problema, estou querendo receber por parâmetro um array de strings contando numeros aleatorios, fiz uns testes dentro do método e o array é inicializado direitinho, o problema é quando eu tento retornar esse array, ele volta como null, da mesma forma que estava antes quando inicializado null, e se eu não inicializo o array como null no main, ele nem vai pro método. O que estou fazendo de errado ? Obrigado !

import java.io.*;
import java.util.Random;

public class trabalho {

public static void main(String args[]){

String NUMEROSGERADOS[] = null;

int CONT;

GERAFORMULARIO(NUMEROSGERADOS);

try{

FileWriter ARQUIVO = new FileWriter(teste.txt);

BufferedWriter BUFFER = new BufferedWriter(ARQUIVO);

for (CONT = 0; CONT < NUMEROSGERADOS.length; CONT++){

BUFFER.write(NUMEROSGERADOS[CONT]);

BUFFER.newLine();

}

BUFFER.close();

}catch (IOException ioe){

System.out.println("Erro: " + ioe.toString());

}

}
public static String[] GERAFORMULARIO(String [] [b]GERA_ARRAYALEATORIO[/b]){
	String AUX;
	int ALEATORIO,CONT;
	Random GENERATOR = new Random();
		GERA_ARRAYALEATORIO = new String[50];
		for (CONT = 0; CONT < 50; CONT++){
			ALEATORIO = GENERATOR.nextInt();
			AUX = String.valueOf(ALEATORIO);
			GERA_ARRAYALEATORIO[CONT] = AUX;
		}		
	return [b]GERA_ARRAYALEATORIO[/b];
}

}

3 Respostas

romarcio

Olá!
Use as tags CODE para postar código fonte, sql, xml…
Não sabe como? Veja aqui: Aprenda a postar Código Fonte no GUJ

C

você não esta recebendo o retorno em uma variável

você também não precisa enviar o array de string pro método

NUMEROSGERADOS[] = GERAFORMULARIO();
G

Valeu craj, resolvi assim, na verdade foi com
NUMEROSGERADOS = GERARFORMULARIO();
mas foi o suficiente, obrigado !!! :smiley:

Criado 9 de dezembro de 2012
Ultima resposta 9 de dez. de 2012
Respostas 3
Participantes 3