Skip to content
Advertisement

Problems using circular array to take inputs and stop/print the last 10 elements once “-1” is entered

I have a Java program that should be doing what the above title describes. Unfortunately, I am now having the issue of having nothing but zeros and a Error message no matter what I input into Scanner.

The code:

 public static void main(String[] args) {

  //Set variable
  int count = 0;
  int numInput = 0;
  int arrSize = 10;

  //Set and initialize string
  int[] numArray = new int[arrSize];

  //Set and initialize Scanner
  Scanner sc = new Scanner(System.in);
  System.out.println("Please input some numbers. Stop by typing -1:");

  while (numInput != -1) {
     numInput = sc.nextInt();
     //Check if input is integer or not
     if (numInput == (int)numInput) {
        if (count == -1) {
           count = numArray.length - 1;
        } else if (count == numArray.length) {
           //Append input to array
           numArray[count] = numInput;
           count++;
        }//End of conditional
     } else {
        //Error message
        System.out.println("Enter a valid integer:");
     }//End of conditional
  }//End of while loop

  //Print array with for loop
  for (int i = 0; i <= 10; i++) {
     System.out.print(numArray[i] + " ");
  }//End of for loop

  System.out.println("n");

  sc.close();

}// End of main

The output:

0 0 0 0 0 0 0 0 0 0 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at last10.main(last10.java:42)

Advertisement

Answer

Like Namandeep_Kaur comment said, your for loop condition is incorrect. You want it to be i < 10 and not i <= 10 because your numArray length is set to 10, however array index is zero-based in Java. So, you will access elements starting with index 0. numArray will have elements at indexes 0-9. Essentially your loop will want to stop when i is equal to 10 because then the condition 10 < 10 is false and you will not try to be accessing the element at numArray[10] which does not exist. Also I have re-factored your program.

import java.util.Scanner;
import java.util.InputMismatchException;

public class LimboProgram {

public static void main(String[] args) {

 //Set variable
 int count = 0;
 int numInput = 0;
 int arrSize = 10;

 //Set and initialize string
 int[] numArray = new int[arrSize];

 //Set and initialize Scanner
 Scanner sc = new Scanner(System.in);
 System.out.println("Please input some numbers. Stop by typing -1:");

 while (numInput != -1) {
  
  try
  {
     // you do not need to check if numInput is a number because 
     // sc.nextInt() will throw an error if the input is not a number.
     numInput = sc.nextInt();
     
     if ( numInput == -1 ) {
        break;
     }
     
     // You can achieve a circular rotation using the modulo (%) operator.
     // So, if count becomes 10, then 10 % 10 results in 0. If count is 11,
     // then 11 % 10 results in 1. This allows you to "circle" the array.
     count = count % numArray.length;
     numArray[count] = numInput;
     count++;
  }
  catch (InputMismatchException e) {
     System.out.println(e);
  }
  
 }


 for (int i = 0; i < 10; i++) {
    System.out.print(numArray[i] + " ");
 }

 System.out.println("n");

 sc.close();

 }

}

I hope this is the result you were looking for in your program. If there’s anything I can clarify further I would be happy to.

Advertisement