# Integer program help

• February 26th, 2012, 08:28 AM
oriordc2
Integer program help
Hi all,
i am new to java and this forum. I am hoping i can get some help with the below program:

Write a program that reads integers, finds the smallest of them and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 6 2 5 2 2 3 0; the program finds that the smallest is 2 and the occurrence count for 2 is 3.
Hint: maintain two variables min and count. Min stores the current min number and count stores its occurrences. Initially assign the first number to min and 1 to count. Compare each subsequent number with min. If the number is less than min, assign it to min and reset count to 1. If the number is equal to min increment count to 1.

Below is what i have started:

import java.util.Scanner;

public class Integers {

/** Main method */
public static void main(String[] args) {

// Create a Scanner
Scanner input = new Scanner(System.in);

int times = 1;

int count = 1;

int min; // The smallest of the integers entered

int minNumber = 6;

System.out.print(
"Enter an int value (the program exits if the input is 0): ");
int data = input.nextInt();
int minimum = data;

// Keep reading data until the input is 0
int sum = 0;
while (data != 0) {
sum += data;

System.out.print(
"Enter an int value (the program exits if the input is 0): ");
data = input.nextInt();
}

//i am not sure where in the program to put in the check for each number against the min number and reset the count as per question above??
if (data < minNumber)
{
minNumber = data;
}

//keep getting 0 for smallest number but do not want to include the 0 as this is just for exiting the input of integers??
System.out.printf( "Smallest Number is %d\n", minNumber );

//not sure if below correct either but i think i am close!!!
for (int i = 0; i < 100; i++)
count++;
count++;
if (minNumber > 1){times++;}

System.out.println("Number " + minNumber + " occurs " + times + " Times ");

}
}
• February 26th, 2012, 10:41 AM
Norm
Re: Integer program help
Do you have any specific questions or problems?
If you get errors, copy and paste them here

If the output is not what you want, copy and paste it here and add comments saying what is wrong with the output and show what you want the output to be.

• February 26th, 2012, 01:33 PM
oriordc2
Re: Integer program help
i have questions below which were in my original post

//i am not sure where in the program to put in the check for each number against the min number and reset the count as per question above??
//keep getting 0 for smallest number but do not want to include the 0 as this is just for exiting the input of integers??
• February 26th, 2012, 01:38 PM
Norm
Re: Integer program help
Quote:

where in the program to put in the check for each number against the min number
You could check each number as you get it to see if it is the smallest number seen so far. If so save it.
When done getting the numbers, you will have the smallest number saved.
Quote:

keep getting 0 for smallest number but do not wan
Test if the value is 0 and do not save it if it is.