ArrayList for String with split [closed]

3

I'm trying to break the following ArrayList with a String :

  ArrayList<Funcionario> funcionarios = new ArrayList();

But when I instantiate the object, I add the values in there through my method and I try to use split , the compiler underlines the .split in red and gives error. I am trying to separate the information by a space " " .

Funcionario func = new Funcionario(); //Instancio o obj
func.cadastrar();  //CHama o método que lê o teclado
funcionarios.add(func); // Manda pro ArrayList
System.out.println("\n\n\nFuncionario cadastrado: \n" + func.toString()); //Mostra o que foi adicionado
System.in.read(); 
func.gravar(); //Salva em um txt
String[] separado = funcionarios.split(" "); //Aqui dá erro

Does anyone know what the error is?

I want to separate the registered information for each employee (name, e-mail, telephone, salary) in% with% s separated.

I want to break in String s to be able to add in a vector, sort and then be able to create a String sorted by name (in one of the application options) or salary (another part of the code).

Thank you

public String toString()
    {
        return    "\nNome: " + nome 
                + "\nEmail: " + email 
                + "\nTelefone: " + telefone 
                + "\nSalario: R$ " + salario ;
    }
    
asked by anonymous 21.11.2015 / 14:35

3 answers

2

You are trying to separate an object with split (). This generates compilation error because this method separates only String. Try this funcionarios.toString().split(" "); , but in the split see what to use to separate.

Hugs!

    
21.11.2015 / 14:47
1

Try this:

List<Funcionario> funcionarios = new ArrayList<>();
SortedSet<Funcionario> funcionariosPorNome = new TreeSet<>(Comparator.comparing(Funcionario::getNome));
SortedSet<Funcionario> funcionariosPorSalario = new TreeSet<>(Comparator.comparingDouble(Funcionario::getSalario));
SortedSet<Funcionario> funcionariosPorEmail = new TreeSet<>(Comparator.comparing(Funcionario::getEmail));
SortedSet<Funcionario> funcionariosPorTelefone = new TreeSet<>(Comparator.comparing(Funcionario::getTelefone));
Funcionario func = new Funcionario(); //Instancio o obj
func.cadastrar();  //Chama o método que lê o teclado
funcionarios.add(func); // Manda pro ArrayList
System.out.println("\n\n\nFuncionario cadastrado: \n" + func.toString()); //Mostra o que foi adicionado
System.in.read(); 
func.gravar(); //Salva em um txt
funcionariosPorNome.add(func);
funcionariosPorSalario.add(func);
funcionariosPorEmail.add(func);
funcionariosPorTelefone.add(func);

If this is not what you want, then please edit the question to make it clear in the text what it is you want.

    
21.11.2015 / 15:14
1

If you want to get employees sorted by different fields you do not need to make these separations. From List<Funcionario> you can sort it as you want.

List<Funcionario> funcionarios = new ArrayList<>();
func.cadastrar();  //CHama o método que lê o teclado
funcionarios.add(func); // Manda pro ArrayList
System.out.println("\n\n\nFuncionario cadastrado: \n" + func.toString()); //Mostra o que foi adicionado
System.in.read(); 
func.gravar(); //Salva em um txt

Sort by name:

Collections.sort(funcionarios, new Comparator<Funcionario>(){

    public int compare(Funcionario f1, Funcionario f2){

        return f1.nome.compareTo(f2.nome);

    }

});

Sorts by salary:

Collections.sort(funcionarios, new Comparator<Funcionario>(){

    public int compare(Funcionario f1, Funcionario f2){

        return f1.salario.compareTo(f2.salario);

    }

});
    
21.11.2015 / 15:38