just in case someone does not know what mode is it is the number that appears most in a set of numbers

The output of the following program is messed up( not just the mode but other parts of it too).

It compiles but the output is not right.

Her is the code:

import javax.swing.*;

public class Array3{
public static void main(String[] args){
double end;
int finish = JOptionPane.YES_OPTION;
int num[]= new int[10];
int mode[] = new int [10];
int numa[] = new int [10];
int y = 0;
int a = 0;
int b = 0;
int c = 0;
boolean ok = false;

for(y = 0; y < 10; y++){
String inputnum = JOptionPane.showInputDialog(null, "Enter a set of ten numbers: \n Enter the numbers one at a time  \n They can be negative, positive, even, or odd");
num[y] = Integer.parseInt(inputnum);
catch(Exception ex)
JOptionPane.showMessageDialog(null, "ERROR: Numerical Data Only");
y -= 1;

num[0] = numa[0];
for(y = 0; y < num.length; y++){
ok = false;
for (c = 0; c < num.length; c++){
if(numa[y] == num [y]){
mode[c] = num[y];
ok = true;
if(ok == false){
num = numa[y];

String someoutput = "Mode: ";
for(a = 0; a< y; a++){

someoutput += "  " + " \n" + num[a] + " " + mode[a];
JOptionPane.showMessageDialog(null, someoutput);
end = JOptionPane.showConfirmDialog(null,"Would you like to play again?","Question Box",JOptionPane.YES_NO_OPTION);
}while(end == finish);


Edited by happygeek: fixed formatting

9 Years
Discussion Span
Last Post by stultuske

It basically shows a line of zeros where the num array should be and a line of 2s where the mode array should be... thanks hope this helps


I'm not sure that I understand the algorithm and what the three arrays (num, numa, mode) are supposed to represent. I understand that the user enters in 10 numbers into num's array, so that makes sense. You immediately overwrite num[0] with numa[0], so the user-entered value of num[0] is lost before you do anything with it. You never initialize the numa array or the mode array. Java initialized them to all 0's it looks like, but I don't know if that is guaranteed. You are comparing a lot of numbers to numa elements, which again were initialized by Java to 0 and are never changed, so basically you are continually comparing elements of num to 0.

I don't understand what the algorithm of the program is.



I'm not enterely sure what you want, but in my opinion, you're making it way too hard.

I've written a piece of code (based on yours) that does what I think you're trying to accomplish :).

You said that the output wasn't right... actually, it was. You just weren't printing what you thought you would be printing, just like VernonDozier already pointed out. Just have a look at the code below, I've added some comments so it won't be a problem following the logic.

package src.uses;

import javax.swing.JOptionPane;

public class Array3{
	public static void main(String args[]){
		// place the number here, it'll be faster to adapt the code if you would like another one
		int number = 10;
		// you'll need two arrays, but to read the numbers, you only need one
		int[] arr = new int[number];
		for(int i = 0; i < number; i++){
			try {
				arr[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a set of " + number + " numbers:\n Enter the numbers one at a time.\n They can be negative, positive, even or odd"));
			} catch (Exception ex){
				JOptionPane.showMessageDialog(null, "ERROR: Numerical Data Only");
				i-= 1;
		// this is the second array you'll use, it has to be the same size as the other one
		int[] mods = new int[number];
		for(int j = 0; j < number; j++){
			// call the getMod(...) method to get the number of times the number is entered by the user
			mods[j] = getMod(arr[j], arr);
		for(int x = 0; x < number; x++){
			// print the elements of both arrays next to each other
			System.out.println("number: " + arr[x] + "      mod: " + mods[x]);
	 * @param number
	 * 		the number for which we want to know how many times it can be found in the array with original input
	 * @param array
	 * 		the array which contains the original input by the user
	 * @return
	 * 		the number of times array contains 'number'
	private static int getMod(int number, int[]array){
		int returnValue = 0;
		for(int i = 0; i < array.length; i++){
			// check for each int in the array whether or not it is equal to the given number
			// if so, add 1 to returnValue
			if(array[i] == number)
				returnValue += 1;
		return returnValue;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.