Aaron
Aaron

Reputation: 151

Enter Integers into an Array and terminating input before reaching limit

I'm doing a practice question in my textbook to add integers (negative and positive) into an array. I want the user to be able to terminate entering numbers into the array before it reaches the end [50].

This is what I've come up with:

The user enters the numbers which is stored in a string. If keepLooping is true and index < size of the array; it will parse token by token the string and place the number into the int array.

There must be an easier way to do this and I can't get my code working, any help would be much appreciated:

// Create Objects to use in program
    Scanner keyboard = new Scanner(System.in);
    int[] arrayOfNumbers = new int[50];


    // Prompt for input
    System.out.println("Enter upto 50 integers separated by a space.");
    System.out.println("Type x to signal no more integers to enter.");

    int index = 0;
    boolean keepLooping = true;

    while (index < arrayOfNumbers.length && keepLooping) {
        String numToAdd = keyboard.nextLine();

        if ( numToAdd.equals("x") || numToAdd.equals("X") ) {
            keepLooping = false;
        }

        if ( !numToAdd.equals("x") || !numToAdd.equals("X") ) {
            arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
        }
    }

    // DEBUG, Print Array
    for (int k=0; k < arrayOfNumbers.length; k++) {
        System.out.println(arrayOfNumbers[k]);
    }

Upvotes: 0

Views: 689

Answers (3)

Georgian
Georgian

Reputation: 8960

If you debugged your program step-by-step (e.g. Stepping with F6 in Eclipse), you would have noticed that index's value does not change. Quickest fix would be:

while (index < arrayOfNumbers.length && keepLooping) {
    String numToAdd = keyboard.nextLine();

    if ( numToAdd.equals("x") || numToAdd.equals("X") ) {
        keepLooping = false;
    }

    if ( !numToAdd.equals("x") || !numToAdd.equals("X") ) {
        arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
    }

    index++;
}


But of course, this solves just the filling-of-array issue. Then come the good practice in programming concerns, which are thoroughly covered by the rest of the answers.

Upvotes: 2

Adam Liss
Adam Liss

Reputation: 48290

You can simplify a little bit with a for loop, and break out of the loop to exit:

for (int index = 0; index < arrayOfNumbers.length; ++index) {
  String numToAdd = keyboard.nextLine();

  if (numToAdd.equals("x") || numToAdd.equals("X")) {
    break;
  }
  arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
}

Upvotes: 2

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

int index = 0;
boolean keepLooping = true;

while (index < arrayOfNumbers.length && keepLooping) {
    String numToAdd = keyboard.nextLine();

    if (numToAdd.equalsIgnoreCase("x")) { // Use EqualsIgnoreCase to shorten it
        keepLooping = false;
    } else { // Use an else statement instead of evaluating the inverse
        arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
    }
    index++; // Increment the index to avoid eternal loop and actually fill the array
}

Upvotes: 0

Related Questions