0

Read whole numbers from a user, then display the list of numbers input and the frequency of each value. The number of inputs should vary from 1-30, and the values accept 0-9.

My issue is that the array always defaults to 1 even if I change it to int[] numbers = new int[numInputs];. It appears that numInputs gets written over to 1 during the first for loop but even changing the value of numInputs it still caps out at 1. I'm sure my logic is wrong but I'm not sure where.


public class Occurrence
{
    public static void main(String[] args)
    {
        //variables
        Scanner keyboard = new Scanner(System.in);
        int numInputs = 1, temp;
        int[] numbers = new int[31];
        int[] count = new int[31];
        boolean success = false;

        //start of program
        System.out.println("How many input values [max:30]?");

        //while no valid input
        while (!success)
        {
            try
            {
                numInputs = keyboard.nextInt(); //get a number
                numInputChecker(numInputs);     //is it valid?
                success = true;                 //ok

            }
            catch (Exception e)                 //else get a new number
            {
                keyboard.nextLine();
                System.out.println("Whole numbers 1 through 30 only, please.");

            }
        }
        //reset the loop checker
        success = false;

        //read numbers to fill that array
        System.out.println("Enter " + numInputs + " numbers.");

        for(int i = 0; i < numInputs; i++)     //from 0 to max number
        {
            while (!success)                   //while no valid number
            {
                try
                {
                    numbers[i] = keyboard.nextInt();    //fill the current cell with a number
                    numberChecker(numbers[i]);          //is it valid?
                    success = true;                     //ok
                }
                catch (Exception e)                     //else get a new number
                {
                    keyboard.nextLine();
                    System.out.println("Whole numbers 0 through 9 only, please.");
                }
            }
        }

       //take the input and count each use of element
        for (int i = 0; i< numbers.length; i++)     //for 0 to max number
        {
            temp = numbers[i];  //get the current value of the cell
            count[temp]++;      //add the use of that value to a new array's cell
        }

        for(int i = 0; i < count.length; i++)   //from 0 to 9 (expected)
        {

            if (count[i] > 0 && count[i] == 1)  //if cell not empty
            {
                System.out.println(i + " " + count[i]);  //print the current cell and how many times it was used
            }
        }

    }

    static void numInputChecker(int integer) throws Exception
    {
        if ((integer < 1) || (integer > 30))    //if 0 or negative, or if 31+
        {
            throw new Exception();              //say no
        }
    }

    static void numberChecker(int integer) throws Exception
    {
        if ((integer < 0) || (integer > 9)) //if negative or 10+
        {
            throw new Exception();          //say no
        }
    }

}

3 Answers 3

3

The logic is broken at the second cycle where you do feel up an array, because success variable isn't reset for a subsequent while loops. It's quite easy to fix it as such:

        for (int i = 0; i < numInputs; i++)     //from 0 to max number
    {
        while (!success)                   //while no valid number
        {
            try {
                numbers[i] = keyboard.nextInt();    //fill the current cell with a number
                numberChecker(numbers[i]);          //is it valid?
                success = true;                     //ok
            } catch (Exception e)                     //else get a new number
            {
                keyboard.nextLine();
                System.out.println("Whole numbers 0 through 9 only, please.");
            }
        }
        success = false; // [amsilf]: That's the cycle reset
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Probably, java.util.NoSuchElementException occurs and catches the error handling the block.

Use keyboard.hasNextInt() before keyboard.nextInt().

Like this!

try {
   keyboard.hasNext(); // here!
   numInputs = keyboard.nextInt(); //get a number
   numInputChecker(numInputs);     //is it valid?
   success = true;                 //ok
}
...

Comments

0

In the last for loop should be that

    for(int i = 0; i < count.length; i++)   //from 0 to 31(expected)
    {

        if (count[i] > 0)  //if cell not empty
        {
            System.out.println(i + " " + count[i]);  //print the current cell and how many times it was used
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.