0

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.

u32i64
  • 2,384
  • 3
  • 22
  • 36
Battybm
  • 23
  • 3
  • Related: http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes?rq=1 –  Feb 16 '17 at 16:41
  • you probably want `if(i == coord1.length){` (Note the `i` not `coord[i]`), `i` is the indice (0, 1, 2, 3, 4, ...) and `coord[i]` is the content at index-i (so coordinates in your case) –  Feb 16 '17 at 16:42

2 Answers2

0

Your problem is that you use a for loop, which is only looping over every element of your array. So, it will never give the user a chance to enter more elements than are in the array. Use a while loop so that instead of looping once for every element, you can loop until the user enters "exit".

Here is a slight modification of your code that should work:

Scanner userInput = new Scanner(System.in);

//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"};

System.out.println("Enter Coordinates: ");
String input = userInput.nextLine();
int arrayIndex = 0;
while (!input.equals("exit")) {

    //convert input to a double
    System.out.println("Enter Coordinates: ");
    double userDouble = Double.parseDouble(input);

    //handle case where array needs to be resized
    if (arrayIndex >= coord1.length) {
        double[] newCoord1 = new double[coord1.length * 2];
        for (int copyIndex = 0; copyIndex < coord1.length; copyIndex++) {
            newCoord1[copyIndex] = coord1[copyIndex];
        }
        coord1 = newCoord1;
    }

    //store the value
    coord1[arrayIndex] = userDouble;
    arrayIndex = arrayIndex + 1;

    //take new input
    input = userInput.nextLine();
}

Keep in mind that this doubles the array size when a resize is needed. That means that if the array size doubles to 6 and the user only enters 5 values, you will have a couple of empty values (zeros) at the end of your array. If that is a problem you can modify it.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • @Battybm happy to help! Please take the time to upvote and accept the answer since it worked out =] – nhouser9 Feb 17 '17 at 07:43
0

You can use a while to loop until the command "exit" is typed by the user. here is an example of dynamically adding elements to the array before each item is added to the array.

    String[] number;
    String userInput = "";
    String[] commands = { "random", "exit", "help"};
    double[] coord1 = new double[0];
    double[] coord2 = new double[0];
    Scanner scan = new Scanner(System.in);
    do {
        System.out.println("Enter Coordinates (ex 1,3)\n");
        userInput = scan.nextLine();

        if(!userInput.equals(commands[1]))
        {
            number = userInput.split(",");
            if(number.length != 2 || !number[0].matches("\\d+") || !number[1].matches("\\d+"))
            {
                System.out.println("Error: Invalid Input! Try again");
            }
            else
            {
                //our index to place our numbers will be the current length of our array
                int index = coord1.length;
                double[] temp = new double[coord1.length + 1];
                //copy the content to the same array but just 1 size bigger
                System.arraycopy(coord1, 0, temp, 0, coord1.length);
                coord1 = temp;
                temp = new double[coord2.length + 1];
                System.arraycopy(coord2, 0, temp, 0, coord2.length);
                coord2 = temp;
                //now use our index to place the numbers in the correct locations
                coord1[index] = Double.parseDouble(number[0]);
                coord2[index] = Double.parseDouble(number[1]);
            }
        }

    } while (!userInput.equals(commands[1]));

    for(int i = 0; i < coord1.length; i++)
    {
        System.out.println(i + " - X: " + coord1[i] + " Y: " + coord2[i]);
    }

Note I use comma seperated values to make the user's life and my life easier, allowing them to enter the coordinates at the same time.

Example output

Enter Coordinates (ex 1,3)
1,5
Enter Coordinates (ex 1,3)
4,7
Enter Coordinates (ex 1,3)
8,9
Enter Coordinates (ex 1,3)
gffd
Error: Invalid Input! Try again
Enter Coordinates (ex 1,3)
3 6
Error: Invalid Input! Try again
Enter Coordinates (ex 1,3)
0,0
Enter Coordinates (ex 1,3)
exit

0 - X: 1.0 Y: 5.0
1 - X: 4.0 Y: 7.0
2 - X: 8.0 Y: 9.0
3 - X: 0.0 Y: 0.0
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26