I need to create an array. The length must be set by using JOptionPane
. Also I must then fill the array with random numbers. This is how far I got.
import javax.swing.*;
import java.util.Arrays;
import java.util.Random;
public class Aufgabe2
{
public String a;
public static void main(String[] args)
{
String a = JOptionPane.showInputDialog("How big should the array be?");
try
{
Integer.parseInt(a);
int meinArray[];
meinArray = new int[a];
Random rand = new Random();
for (int i = 0; i < a; i++)
{
meinArray[i] = rand.nextInt();
}
System.out.println(Arrays.toString(meinArray));
}
catch (NumberFormatException e)
{
System.out.println(a + " isn`t a valide input. Please insert a number");
}
}
}
You need to store the result of Integer.parseInt(a)
to some variable and then use it e.g.
int x= Integer.parseInt(a);
int meinArray[];
meinArray = new int[x];
Random rand = new Random();
for (int i = 0; i < x; i++){
meinArray[i] = rand.nextInt();
}