About inserting names into an array. The user puts the course name as argument in the method

Asked

Viewed 104 times

3

I have this method:

public void inserirCurso(String nome){
    System.out.println(" Cadastre seus cursos aqui.");
    for(int totalCursos = 0; totalCursos < cursos.length; totalCursos++){
        cursos[totalCursos] = nome;
    }
}

I’m calling the method that:

aluno.inserirCurso("matematica");
aluno.inserirCurso("historia");
aluno.inserirCurso("ciencias");
aluno.inserirCurso("informática");
aluno.inserirCurso("português");
aluno.listarCurso();

It’s printing like this:

Cadastre seus cursos aqui.
Cadastre seus cursos aqui.
Cadastre seus cursos aqui.
Cadastre seus cursos aqui.
Cadastre seus cursos aqui.
português
português
português
português
português

I found it strange because the message "Register your course here." It’s out of the for. What I did wrong how can I fix this?

The list

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

Now it’s being printed like this after the whole Bears++:

português
null
português
null
português
  • I didn’t understand the question, I was ready to use it. The solution given by Darkhyudra is only to go back to what I had shown in http://answall.com/q/108701/101

  • @bigown really, now also I was in doubt. I had not seen that question until then.

3 answers

4

Its first 5 lines containing Cadastre seus cursos aqui. are being printed by the 5 method calls aluno.inserirCurso(). Hence, within this method you fill a variable with the value passed in the argument, the problem is that each time you call this method you delete everything that had already been written in it up to the moment and fill in with the new value. Therefore, it will only keep the value of the last passed value as an argument that in the case of your example is português.

You haven’t shown us your method aluno.listarCurso();, but he’s probably just a guy who travels the vector cursos. So he prints all the contents of it, for 5 consecutive times, which is the argument of the last call of the method, ie: português.

To fix this, it depends a little on what you want to do exactly, but you probably want some Arraylist-like structure instead of the vector cursos, where your method of inserting courses would be so simple:

public void inserirCurso(String nome){
    cursos.add(nome);
}

I don’t know where you declared it, but it would look like this:

List<String> cursos = new ArrayList<String>();

While your method of listing courses will probably not undergo major modifications.

  • Hummm interesting, but with lists it is very easy yet I need to better understand this vector. Here’s the question...

  • @Alinegonzaga the arraylist is just a suggestion, the important thing is you understand why the error happened and think of the best way for you to avoid it. If you need help with how your structure is going to look from now on, you can give more details on how you want it to look. Darkhyudra’s code hint seems to be closer than you want, take a look at it.

3


Your inserirCurso() is that displays on the screen Cadastre seus cursos aqui several times, since you are calling several times the method.

Your insertion method is also not correct, use

for(int totalCursos = 0;...cursos[totalCursos] = nome;

means that it will always overwrite all vector items from position 0 of the array to position 0, saving only the last insertion. That’s why you printed it out português over and over again.

Solution

If not to use any kind of object like Arraylist, the most basic you could do is:

public void inserirCurso(String nome){
    //System.out.println(" Cadastre seus cursos aqui.");
    cursos[totalCursos] = nome;
    totalCursos++;

}

But please note that a code like this does not do any treatment to prevent the index from being out of vector size.

  • It will overwrite the whole vector, not just the 0 position. It’s not even?

  • If the vector already had some content, yes. Another thing is that we don’t know the size of it, but imagining that he printed Portuguese 5 times, he probably created the vector already with 5 positions. I’ll give you a little dit.

3

I don’t think your approach to vector use is correct in this code. You should check that the Dice is not busy before entering a new course, the way it is, as @Math and @Darkhyudra said, will register only the last.

From your method of inserirCurso(), you can check if the Dice is filled in before assigning a value to it. When you instantiate a vector, it is created with all indices like null, then you can do the next check:

public void inserirCurso(String nome) {
    System.out.println(" Cadastre seus cursos aqui.");
    for(int totalCursos = 0; totalCursos < cursos.length;totalCursos++) {
        if(cursos[totalCursos] == null) {
            cursos[totalCursos] = nome;
            break;
        }
    }
}

This way, only an Indice that has not yet been assigned a string will be filled in. See working on ideone.

Note that it was possible to check an empty tile like null because it is a String vector, the above method would not work with primitive types.

Browser other questions tagged

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