Arraylist for String with split

Asked

Viewed 1,653 times

3

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

  ArrayList<Funcionario> funcionarios = new ArrayList();

But when I instate the object, I add the values inside through my method and try to use the split, the compiler underlines the .split in red and gives error. I’m 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

Someone knows what the mistake is?

I want to separate the entries for each employee (name, email, phone, salary) in Strings separate.

I want to break into Strings to be able to add in a vector, sort and then create a ArrayList 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 ;
    }
  • You want a list of employees or a list of employee names?

  • You should have posted the error. But in this case, the error is obvious: there is no method ArrayList.split.

  • What is the purpose of System.in.read(); isolated in the middle?

  • The System.in.read(); is to pause the screen, otherwise it passes straight. This process is inside a while

  • You want a list of employees or a list of employee names?

  • Opa @Victorstafusa I want to separate the entries for each employee (name, email, phone, salary) in separate strings.

  • @This completely changes your question. In this case, this type of information should be present in the body of the question, otherwise you will end up not getting the answers you seek.

  • How is the method toString() class Funcionario?

  • 1

    You want 4 Arrays, one with the names other with the email etc?

  • @Victorstafusa I added toString() used

  • @ramaral This. I want to break into strings to be able to add in an array, sort and then be able to create an Arraylist sorted by name (in one of the application options) or salary (another part of the code).

  • 1

    You are already using JAVA 8?

Show 7 more comments

3 answers

2


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

Hug!

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 that’s not what you want, then please edit the question to make it clear in the text what it is you want.

1

If what you want is to get employees ordered by different fields do not need to make these breakups.
From a List<Funcionario> you can order it however 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);

    }

});

Ordains by salary:

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

    public int compare(Funcionario f1, Funcionario f2){

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

    }

});
  • Perfect, only unfortunately I can’t use any Sort method. It’s a college job. Sorry, I forgot to put that in the statement.

  • 2

    With this "forgetfulness" made at least 2 people waste 30 minutes of their time.

  • 1

    @Maximilianomeyer Honestly, it seems like you forgot to put everything in the statement! Edit the question and put there everything she should have.

  • Good, guys. Don’t bother. I ask the administrator to remove the topic. Sorry for the "waste of time". It was not my intention. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.