Home > Software design >  do While loop crashes at second input
do While loop crashes at second input

Time:10-27

I'm attempting to make a program that takes and returns distinct values in an Array. Funnily enough, the program itself works, but when I try to add a do while sentinel loop, the program crashes at the second attempt to input values. The IDE is not returning an error so I'm trying to see if my logic may be wrong somewhere?

import java.util.Scanner;

public class DistinctValuesCourtney {

    static int n = 0;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        
        int[] distinct;
        int i;
        int[] array;
        array = new int[10];
        Scanner input = new Scanner(System.in);
        int largestValue;
        char doAgain;
    
                do {
                System.out.print("Please enter 10 integers for the array:");  //
                
                    for (i = 0; i < array.length; i  )
                    {
                    array[i] = input.nextInt(); 
                    }
                    
                
                
                distinct = getDistinct(array);
                
                    
                System.out.print("You entered these values ");
                        for (int index = 0; index < n; index  ) //use index instead of i
                        {
                             System.out.print( distinct[index]   " ");
                        }
                
                
                System.out.println("Would you like to retry? Y/N: ");   
                    doAgain = input.next().charAt(0);
                }
        
    
        
            while ((doAgain == 'Y') || (doAgain == 'y'));
                System.out.println("Have a nice day!");
        
            }
        
    public static int[] getDistinct(int[] inputArray)
    {       
        int[] finalArray = new int[10];
                
        for (int i = 0; i < inputArray.length; i  )
        {
           
            int j;                                                      //see if this is printed
            for (j = 0; j < i; j  )
            if (inputArray[i]== inputArray[j])
                break;
     
           
            if (i == j)
            {
            finalArray[n  ] = inputArray[i];    
            //System.out.print( inputArray[i]   " ");
            
            }
        }
        return finalArray;
    }
}

CodePudding user response:

You will get an ArrayIndexOutOfBoundsException in your getDistinct() method as n is static and will be greater 9 the second time you enter numbers. For that reason one should generally avoid to use global parameters if you don't need them. Global state can cause all sort of issues.

You can just add this line (int n = 0;) before your for loop in the getDistinct() method and your code will work.

public static int[] getDistinct(int[] inputArray)
{
    int[] finalArray = new int[10];
    int n = 0; // use a local variable here which will have value 0 everytime you execute the method!
    for (int i = 0; i < inputArray.length; i  )
    {
        int j;
        for (j = 0; j < i; j  )
            if (inputArray[i]== inputArray[j])
                break;
        if (i == j)
        {
            finalArray[n  ] = inputArray[i];
            //System.out.print( inputArray[i]   " ");
        }
    }
    return finalArray;
}

Just FYI: there are quicker/ better ways to eliminate duplicates e.g. using sorting with an appropriate algorithm or hashing.

  • Related