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 9 of 9

Thread: occurrence of int in array..

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default occurrence of int in array..

    Can i have code for the given problem please......

    (Count occurrence of numbers) Write a program that reads the integers between 1
    and 100 and counts the occurrences of each. Assume the input ends with 0. Here is
    a sample run of the program:
    Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
    2 occurs 2 times
    3 occurs 1 time
    4 occurs 1 time
    5 occurs 2 times
    6 occurs 1 time
    23 occurs 1 time
    43 occurs 1 time

    Note that if a number occurs more than one time, the plural word “times” is used
    in the output.


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

    Default Re: occurrence of int in array..

    Can you post the code you have written for this program and any questions about the problems you are having?

    Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: occurrence of int in array..

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: occurrence of int in array..

    This is the following code i have written.. i am bit new to programming so please help me out.
     public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int[] num = new int[100];
            int count = 0;
     
            System.out.println("Enter the integers between 1 and 100");
              for(int i = 0 ; i < num.length ; i++){
                num[i] = input.nextInt();
            if(num[i] == 0)
                break;
              }
        for(int i = 0 ; i < num.length ; i++)
        {
            for(int j = 0 ; j > i ; j++)
            {
                if(num[i] == num[j])
                    count++;
               }
           for( i = 0 ; i < num.length ; i++)
         System.out.println(num[i] + " occurs " + count + " times ");
        }
        }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: occurrence of int in array..

    Can you explain what the problem is with the code you posted?
    Post the program's output
    and also post what you want the output to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: occurrence of int in array..

    My output should be like you see in the first post ....
    i just need the code to get above output..

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: occurrence of int in array..

    What happens when you compile and execute the program? Copy the full contents of the console from when the program is compiled and executed and paste it here.

    What part of the assignment is working?
    What steps are you having problems with?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: occurrence of int in array..

    I'm getting the following output:

    Enter the integers between 1 and 100
    2 3 5 6 2 4 3 8 6 2 0
    2 occurs 0 times
    3 occurs 0 times
    5 occurs 0 times
    6 occurs 0 times
    2 occurs 0 times
    4 occurs 0 times
    3 occurs 0 times
    8 occurs 0 times
    6 occurs 0 times
    2 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    0 occurs 0 times
    BUILD SUCCESSFUL (total time: 8 seconds)

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: occurrence of int in array..

    Did you see my questions in the last post?
    1)What part of the assignment is working?
    2)What steps are you having problems with?

    How should the code use the int[100] array for counting what the user enters? Why is the array so large if the user is only going to enter a few numbers? Should the array hold the numbers that the user enters? What if the user wanted to enter 200 numbers?

    Did your instruction tell you about how to use a number to index into an array to get at an element in the array? Could the number that a user enters be used as an index to the array?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Trying to compare an int variable to an index in an array
    By TCDave in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2012, 07:46 AM
  2. Bubble Sort Int Array Problem
    By thisbeme in forum Collections and Generics
    Replies: 1
    Last Post: September 11th, 2011, 09:24 AM
  3. chr array to int
    By Joy123 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 7th, 2011, 06:51 AM
  4. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM

Tags for this Thread