So I have an assignment where I take in doubles from the user in the console and then store those doubles in an array. These doubles will actually be coordinates which I will use later on.
The program should be able to take an unknown amount of doubles from the user. The problem I am having is allowing the array size to grow dynamically. We CANNOT use arrayList or any java library collection classes. Here is what i have so far:
import java.util.Scanner;
public class testMain{
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
boolean debug = true;
//Two array's where i'll store the coordinates
double[] coord1 = new double[3];
double[] coord2 = new double[3];
//Array for user commands
String[] commands = { "random", "exit", "help"};
for(int i = 0; i < coord1.length; i++){
System.out.println("Enter Coordinates: ");
double index = userInput.nextDouble();
//If more doubles needed for array we want to resize array
if(coord1[i] >= coord1.length){
for(int j = 0; j < 10; j++){
coord1[j] = j + 10;
}
double newItems[] = new double[20];
System.arraycopy(coord1, 0, newItems, 0 ,10);
coord1 = newItems;
}
coord1[i] = index;
}
if(debug == true){
printArray(coord1);
}
}
public static void printArray(double arr[]){
double n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
I can't seem to figure out how to recognize when the reaching the end of coord1 to execute code to increase the size and continue looping for more doubles.
Later on when the user is done an empty entry from console should exit loop and display the array.