Hello eric and welcome to the Java Programming Forums.
You can use this code below. I have used the Scanner class to take user input from the console.
This will then set the size of the array depending on what int value the user enters:
import java.util.Scanner;
public class Eric {
public static void main(String[] args) {
System.out.println("Enter Array size: ");
Scanner sc = new Scanner(System.in);
int arraySize = sc.nextInt();
String[] myArray = new String[arraySize];
}
}
To take this one step further, ive added this code so the user can now populate each array field and once its filled it will print its results:
import java.util.Scanner;
public class Eric {
public static void main(String[] args) {
System.out.println("Enter Array size: ");
Scanner sc = new Scanner(System.in);
int arraySize = sc.nextInt();
String[] myArray = new String[arraySize];
System.out.println("Array size set to " + arraySize);
//Populate the array
for(int a = 0; a < myArray.length; a++){
System.out.println("Enter Array value [" + a + "]:");
myArray[a] = sc.next();
}
//Print array values. Can also add Array sort code here.
for(int b = 0; b < myArray.length; b++){
System.out.println(myArray[b]);
}
}
}
To sort the Array you could use the java.util.Arrays class:
http://www.javaprogrammingforums.com...ays-class.html
Let me know if you get stuck..