Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Integer program help

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;


    // Read an initial data
    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;

    // Read the next 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 ");


    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Integer program help

    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.
    keep getting 0 for smallest number but do not wan
    Test if the value is 0 and do not save it if it is.

Similar Threads

  1. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  2. Use of Integer.parseInt
    By tarkal in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:37 AM
  3. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  4. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 06:01 AM
  5. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM