Why does Netbeans suggest changing an array addition?

Asked

Viewed 182 times

1

I am starting my studies in programming with Java. I am creating a class for object in Netbeans that warns the following message:

Array concatenated with String

Why?

My code:

import java.util.Arrays;

public class PessoaGenerica {
    int idade[]
    String nome[];
    String cidade[]

    void dizerNome(){
       System.out.println ("OLá, meu nome é" + Arrays.toString(nome));
    }

    void dizerIdade(){
       System.out.println ("OLá, meu nome é" + Arrays.toString(nome));
    }

    void dizerCidade(){
       System.out.println ("OLá, meu nome é" + Arrays.toString(nome));
    }
}

SS

  • 1

    Why are you trying to convert an integer vector into a string? And that’s not how you display an array, it’s using repetition loop.

  • 1

    Since you are starting it would be good to read this to learn things the right way: http://answall.com/q/101691/101. Try to put your codes as text, it makes it easier for everyone. Actually, this code doesn’t do anything close to what you think it does. The suggestion is wrong because the code makes so little sense that Netbeans doesn’t even understand what it actually wants.

  • Did the answer solve your problem? Do you think you can accept it? If you don’t know how you do it, check out [tour]. This would help a lot to indicate that the solution was useful to you and to give an indication that there was a satisfactory solution. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

3

I guess I could start reading what is a vector.

It even makes sense to pass the whole vector as a parameter, but it makes little sense to access it directly. Normally we access its elements through an index. So it would make sense to do, for example nome[0] for the first element of the vector, or nome[1] to the second.

I could even access it through a variable if it exists somewhere. It could be a loop that goes varying to catch all the elements (although in this case has for that doesn’t even need to pick up the index, but this is more advanced subject): nome[i].

Of course, to access an element you must have created it. In this code you are creating nothing. It’s not even reserving room for the vectors to have elements. Ideally they should be declared with the maximum amount of elements it can support. Or use a ArrayList that allows increasing the number of elements as needed.

It’s still a little weird having a vector for nome, another to cidade and another to idade. This information seems to be correlated and should be together in a separate structure, and the vector would be of the type created with these members. I won’t go into detail because it seems to be advanced to what it knows. But that’s why I should go with simpler examples, maybe look for a book that better directs you to each thing to learn. For now try using vector only for one thing at a time and hope to better understand the creation of classes to create vectors with compound information.

Actually watching the class, I don’t even know if there should be vectors in there. The problem may be this, it may have declared the members of the class as vectors and it was not what it wanted. Try taking the clasps out and see what happens (obviously take the Arrays.toString() also). Maybe this is the right way. Once you understand how this works, you can think about creating vectors in another class. Then I don’t think it’s okay to have vector. If that was the intention, I think you’re learning to do it the wrong way. I can’t imagine why I would want to keep several people inside a class that has a name that seems to be one person.

I reinforce the idea of looking for a structured and quality way of how to do each thing, which teaches the basics with good teaching and without being a cake recipe. I know it is not easy to find, you have to be very careful because there is a lot of bad stuff out there. Here it is not suitable to ask for references about this. Start with familiar books. Be critical with everything. Try different fonts. Ask about specific questions you already have experience with. can be here, for this the site is very good.

Browser other questions tagged

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