Using Java 8+, you can generate the array with the squares in a single row. Assuming your array is the variable numeros
, then you can do it:
int[] quadrados = IntStream.of(numeros).map(x -> x * x).toArray();
To read the 20 numbers, you can also do it with two lines:
Scanner entrada = new Scanner(System.in);
int[] numeros = IntStream.generate(entrada::nextInt).limit(20).toArray();
To display the array, you can do it in a row as well:
System.out.println(Arrays.toString(quadrados));
Here is the resulting complete code. Note that it is very simple, only needs 5 lines between the {
and the }
of the method main
:
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
class Teste {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
int[] numeros = IntStream.generate(entrada::nextInt).limit(20).toArray();
int[] quadrados = IntStream.of(numeros).map(x -> x * x).toArray();
System.out.println(Arrays.toString(numeros));
System.out.println(Arrays.toString(quadrados));
}
}
Given this input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
This output is produced:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
See here working on ideone.
Ah, Math.sqrt(...)
is to compute the square root. To compute the square, the simplest way is to multiply each element by itself as in the code above. Note that the code to create the array with the squares works regardless of the size of the original array.
If you want a message to appear to ask for the number, you can do something like this:
int[] numeros = IntStream.generate(() -> {
System.out.println("Digite um número: ");
return entrada.nextInt();
}).limit(20).toArray();
Would this variable c be a counter? matrices one and num2 have the same size? Post more lines of code! What error happens?
– Antonio Santos
The variable c is the counter of the first vector, they have the same size. the code: import java.util.Scanner; public class Exercicio01list { public Static void main(String[] args) { int [] num = new int[5]; int [] num2 = new int[5]; Input scanner = new Scanner(System.in); for (int c = 0; c<num.length; c++){ System.out.println("Number: "); num[c] = input.nextInt(); } for (int i = 0; i<num2.length; i++){ num2[i] = Math.sqrt(num[c]); } } }
– punkoco
Then take out this counter that is not needed. Put a[i] in place.
– Antonio Santos
put i in place of num[i] and it hasn’t worked yet, the error is this one: Exception in thread "main" java.util.Inputmismatchexception at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at exercicio01lista.Exercicio01lista.main(Exercicio01lista.java:15) C: Users Bruno Appdata Local Netbeans Cache 8.2 executor-snippets run.xml:53: Java returned: 1 BUILD FAILURE (total time: 9 seconds)
– punkoco
Without knowing what the numbers are, what the size of each vector is and what error is occurring, we have no way of guessing. I suggest you read the [Ask] page, how to mount a [mcve] and the Manual on how not to ask questions. Then click on [Edit] and add the missing information, because only with this code and without knowing what the error is, we have no way to help. Something else, the
sqrt
returns the square root. If you want the square, useMath.pow(valor, 2)
(or just multiply the value by itself)– hkotsubo
Use the link [Edit] to put this information in the question, is more readable than in the comments
– hkotsubo
The error message mentions
Scanner.nextInt
, and if he made a mistake it is because he expected a number but was provided something else. Please put the code that has theScanner
and the data he’s reading– hkotsubo