About inserting names into a vector. The user places the course name as argument in the method

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 like this:

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

You are 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 that the message "Register your course here." It's out of the for. What have I done wrong how can I fix this?

Or list

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

Now it is being printed like this after the totalCourses ++:

português
null
português
null
português
    
asked by anonymous 20.01.2016 / 18:18

3 answers

3

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

Your insert method is also not correct, use

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

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

Solution

If you are not going to use any type of object like ArrayList, the most basic thing you could do is:

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

}

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

    
20.01.2016 / 18:25
4

Your first 5 lines containing Cadastre seus cursos aqui. are being printed by the 5 calls of the aluno.inserirCurso() method. Hence, within this method you fill in a variable with the value passed in the argument, the problem is that every time you call this method you delete everything that has already been written on it so far and fills in with the new value. Thus, it will only keep the value of the last value passed as an argument which in the case of its example is português .

You have not shown us your aluno.listarCurso(); method, but it's probably just a for loop that runs through the cursos vector. So it prints the entire contents of it, for 5 consecutive times, which is the argument of the last method call, that is: português .

To fix this, it depends a bit on what you want to do exactly, but you probably want some structure similar to an ArrayList instead of the cursos vector, where your method of entering courses would look as simple as this:

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

I do not know where you stated it, but it would look like this:

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

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

    
20.01.2016 / 18:23
3

I think your approach to using vectors is not correct in this code. You should check if the index is not busy before entering a new course, as it is, as @Math and @DarkHyudrA have spoken, will only register the last one.

From your inserirCurso() method, you can check if the index is filled before assigning a value to it. When you instantiate a vector, it is created with all indices as null , so you can do the following verification:

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 will only fill an index that has not yet been assigned a string. See working at ideone .

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

    
21.01.2016 / 11:17