0

I'm writing a program that takes 10 numbers as input and displays the mode of the numbers using parallel arrays and a method that takes an array of numbers as a parameter and returns the value that appears most often in the array. PROBLEM: However, when I run my program it has absolutely no output, also, I am not sure how to implement the parallel arrays. Does anyone know? Thank you.

import java.util.Scanner;

public class Mode {

public static void main(String[] args) {
}

public int computeMode(int[] nums) {
    Scanner scanner = new Scanner(System.in);

    int maxValue = -1;
    int maxCount = 0;
    int x = 0;
    //count how many times nums[i] appears in array

    System.out.println("Enter 10 numbers: ");
    for (int i = 0; i < 10; i++) {

        try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
            x = scanner.nextInt();
        } catch (Exception e) {
            System.out.println("Invalid input! Please reenter 10 integer values.");
            scanner = new Scanner(System.in);
            i = -1;

            continue;
        }
        for (i = 0; i < nums.length; i++) {
            int count = 0;
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] == nums[i]) {
                    count++;
                }
            }

            if (count > maxCount) {
                maxValue = nums[i];
                maxCount = count;
                System.out.println("The mode is: " + maxValue);
            }

        }

    }
    return maxValue;
}

}

0

2 Answers 2

2

The main function is empty, so it does not anything, and the function doesn't need any parameter, because you read the numbers with the scanner

I think you need this:

import java.util.Scanner;

class Mode {
    public static void main(String[] args) {
        computeMode();
    }

    public static void computeMode(){
        int nums[]=new int[10];
        Scanner scanner = new Scanner(System.in);

        int maxValue = -1;
        int maxCount = 0;
        int x = 0;
        //count how many times nums[i] appears in array

        System.out.println("Enter 10 numbers: ");
        for (int i = 0; i < 10; i++) {

            try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
                x = scanner.nextInt();
                nums[i]=x;
            } 
            catch (Exception e) {
                System.out.println("Invalid input! Please reenter 10 integer values.");
                i =i -1;
                scanner.nextLine();

                continue;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            int count = 0;
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] == nums[i]) {
                    count++;
                }
            }

            if (count > maxCount) {
                maxValue = nums[i];
                maxCount = count;

            }

        }
    System.out.println("The mode is: " + maxValue);
    }


}
Sign up to request clarification or add additional context in comments.

2 Comments

Aha this works, thank you. Do you happen to know if this method uses parallel arrays? And how I would edit the exception to catch only letters or symbols (anything that is not a number as the program simply specifies number...which I assume means either an integer or a decimal). @TurtleLoop
There is only one array (nums[]), then this method doesn't use parallel arrays. Here you have an example of parallel arrays: mathbits.com/MathBits/Java/arrays/ParallelArrays.htm
0

The code writes x, but never reads it; reads nums, but never writes it; and implements computeMode, but never calls it.

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.