Doubt about filling in Java Array

Asked

Viewed 882 times

1

I’m having trouble solving the following exercise:

  • Create a class with a main method;
  • Create an int array with 10 positions and popule using for;
  • Print all array values and, for even values, display the value followed by the word "even" (use % operator to know when it is par)

First I did so, but it is giving compilation error:

public class MeuArray {
    public static void main (String[] args) {
        int[] MeuArray = new int[10];
        for (int i = 0; i < 10; i++){
            if (MeuArray[i] % 2 == 0) {
         System.out.prinln("O vetor é par:", MeuArray[i]);
        } else {
            System.out.println("O vetor é impar:" MeuArray[i]);
        }

 }
 }
}

I switched to this code and the outputs are showing the result 0:

public class MeuArray {
    public static void main (String[] args) {
        int[] MeuArray = new int[10];
        for (int i = 0; i < 10; i++){
            if (MeuArray[i] % 2 == 0) {
         System.out.println(MeuArray[i]);
        } else {
            System.out.println(MeuArray[i]);
        }

 }
 }

}
  • 1

    What about when you populate the array? Notice you’re already creating it and displaying it without populating it?

  • The code is not wrong, you just forgot to do what was requested in the second exercise item, which is popule using for

  • 1

    Yes, I noticed, I don’t know how to populate an array with for in java, I’m beginner.

  • 1

    The first code does not compile because you used prinln instead of println, and it would be right to have a + between the text and the value: "O vetor é impar:" + MeuArray[i] instead of "O vetor é impar:" MeuArray[i] and "O vetor é par:" + MeuArray[i] instead of "O vetor é par:", MeuArray[i]. About the second code, the array was missing, as already commented. You need to make two loops for, one to popular the values, and the other to print them

2 answers

2


Your second code is not wrong, but as I mentioned in the comments, you have barely created the array, and without populating it, you are already scanning its values.

The result is always zero because an array of the primitive type int, when created, it is already filled with 0 as the starting value in all positions.

To populate an array, the logic is the same that you are already using to scan it, create a loop from position 0 to position tamanhodoarray-1 and assign values to each position, would be something similar to the below:

for(int i = 0; i < meuArray.length; i++){

    meuArray[i] = <valor que voce ira atribuir>;

}

Remember that if you use a literal value to assign the position of the array, all positions will be equal, the exercise does not speak in user input, but if allowed to use, Voce can use the Random class to generate random numbers.

Another point recalled by the user @hkotsubo is how you are concatenating values within the println(), to concatenate strings in java, if you use the symbol + and not , or space, even if you had done the spelling of the printing method correctly, would give compilation error for this reason.

The correct shape would be something like below:

System.out.println("isto é uma string literal" + variavelQualquer)
  • It worked, thank you so much for your help!

-1

What do you want to put in the array? first the Array you created has 10 positions, you can fill it like this:

Meuarray[i] = i;

in this way you will fill the position i, which in the case is 0 to 9; with the very number of i.

puts this command under the for command, on top of the first if and see what happens.

  • This is not the only problem of the question. I suggest you read it carefully to confirm

  • I understand, I just wanted to solve in a basic way by filling the array with the number of the position itself, using the rest it already discovers which is even and which is odd.

  • Don’t get me wrong, but the solution you gave there does not solve the problem, only creates a complexity that the author, as said to be beginner, would be more doubtful yet. But if you think the answer answers, fine, we don’t have to debate it. It was just a hint, the option to choose whether to accept it or not is yours.

  • Do you think that what I said creates complexity? If you were bothered and included my answer as not useful, it is your opinion. My intention was to help and I think it was clear what to do and why.

  • I was not bothered Camilo, it was just an opinion about what the answer could have to make it more complete for the author. As I said earlier, don’t get me wrong, it was just a tip aimed at improving the content of the post, if you disagree, all right, no problems, we are a free community, disagree is part ;)

  • @Camilo thank you for your solution, I did not find complex, she also complemented the first one I received. Because from what I understood of the statement, it was to fill the vectors automatically using the for. In the first solution I would have to assign the values, with their values were filled automatically according to the numbering of the counter.

Show 1 more comment

Browser other questions tagged

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