How to insert multiple values into an array?

Asked

Viewed 9,108 times

3

A student can take several courses such as "English", "Portuguese" etc. How do I put multiple course names in one array? I don’t want to set an example with ArrayList. It’s just to take a question of my own. Then I’ll have to create the methods to add, remove, view and edit? How to do?

PS: AlunoPrivado extends Aluno.

The problem is I’m not able to do it. I did so:

aluno.setCursos("matemática");

But it’s wrong, how do I do it?

 public class Aluno {

   private String nome;
   private String matricula;
   private int idade;
   private String[] cursos = new String[5];

   // [...]gets sets
 }

And I have Start class:

public class Start {

    public static void main(String[] args) {

       AlunoPrivado aluno = new AlunoPrivado();
       aluno.setNome();
       aluno.setCursos("matemática");
    }
}
  • I don’t quite understand what you want. The array is already created. Do you want to create the methods that add and remove the courses that the student can take, one by one? What do you call editing?

  • Help me? -- vector is to include remove list and edit course for the student. Do you understand? Editing would be like editing a course that’s in a position....

  • See if I am on the right path to do the other parts. Whenever possible put all the code you have that is necessary. This helps to understand the problem better and in this way I could test and give a working version.

2 answers

8


Remember that as everything is private, the class that will inherit this will not have access to these members. Maybe you want to change to protected.

You’ll add that to the class Aluno:

private int totalCursos = 0;
public void addCurso(String curso) {
    if (totalCursos == 5) {
        throw Exception("Quantidade de cursos chegou ao limite");
    }
    cursos[totalCursos] = curso;
    totalCursos++;
}

If you want to add several:

public void addCursos(String ... listaCursos) {
    if (totalCursos > 5 - cursos.Length) {
        throw Exception("Quantidade de cursos chegou ao limite");
    }
    for(String curso : listaCursos){
        cursos[totalCursos ] = curso;
        totalCursos++;
    }
}

Of course you can do it a different way. But that’s the idea. If it were more than an exercise, other cares would probably need to be taken. If you could do otherwise outside the specified requirement, you would do better. Launch Exception is not the correct in normal code, but to create a new exception just for this in a simple exercise is exaggeration. I would do without exception, but this is not the culture of Java. So:

public Boolean addCurso(String curso) {
    if (totalCursos == 5) {
        return false;
    }
    cursos[totalCursos] = curso;
    totalCursos++;
    return true;
}

Note that I preferred to use a name starting with add to reflect what you are adding. If you prefer, switch to adiciona.

To list:

public void listCursos() {
    for(String curso : cursos) {
        System.out.println(curso);
    }
}

Modify:

public boolean changeCurso(String cursoModificar, String cursoNovo) {
    for(String curso : cursos) {
        if (curso.equals(cursoModificar)) {
            curso = cursoNovo;
            return true;
        }
    }
    return false;
}

Another direct version by the position of the course in the register:

public boolean changeCurso(int cursoModificar, String cursoNovo) {
    if (cursoModificar < 0 || cursoModificar > totalCursos - 1) {
        return false;
    }
    cursos[cursoModificar] = cursoNovo;
    return true;
}

Remove:

public boolean removeUltimoCurso() {
    if (totalCursos == 0) {
        return false;
    }
    cursos[totalCursos - 1] = null;
    totalCursos--;
    return true;
}

public boolean removeTodosCursos() {
    cursos = new String[5];
    return true;
}

I put in the Github for future reference.

You can do the removal by position and by search, same as what was done in the modification, but then you need to establish a cleaning criterion array, which although relatively simple, is not so trivial for those who are starting.

  • resetting the array would be difficult?

  • It’s done. Unless you’re talking about something else.

5

You can use this method, which adds the course names to the vector cursos, replacing the previous values, if there were any:

public void setCursos(String ... novosCursos){
    int i = 0;
    for(String curso : novosCursos){
        cursos[i] = curso;
        i++;
    }
}

To use you just need to separate each parameter from the type String each other by comma:

aluno.setCursos("matemática", "português", "inglês");
  • 1

    Thank you! I get it.

Browser other questions tagged

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