Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Counting Occurences from Inputted Integers HELP!

    [B]Hi all! I'm new to java and this is one of our final requirements in school: 1 dimensional arrays. Here's the code and I commented the part I'm stuck in. Kindly help anyone!

    PROBLEM DEFINITION: Ask the user how many numbers he would like to input. Accept numbers and store in an array. Maximum of 20 numbers only. Access the array and count the occurences of each inputted integer. Print in a table labeled NUMBERS and OCCURENCE in separate columns.

    Here's my code:

    Counting Occurences from Inputted Integers HELP!-forum.jpg
    Last edited by ProgrammingNoob; 09-27-2013 at 12:19 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Counting Occurences from Inputted Integers HELP!

    I must say that this is a nice challenging assignment. It would be a waste if someone would be telling you what to do to solve it, you won't learn anything in the process. The most important thing to learn in programming is the way to think, not the actual code itself. Once you can think in the right way, the code comes naturally.

    Given that statement: you must have some idea of what to do, even when you can't figure out how exactly to do it in code just yet. So share what you have come up with yourself so far.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Counting Occurences from Inputted Integers HELP!

    Hi thanks for replying! Here's what I'm thinking of but can't write in code! XD

    1. Access the elements of the array and compare the previous index with the elements of all the other indeces.
    2. Find a match of equal numbers and increment a counter.
    3. Print the elements just once each and print their occurence via the counter.

    That's it so far. I just can't seem to write 'em in code!

  4. #4
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Counting Occurences from Inputted Integers HELP!

    This is all the algorithm I can muster for the accessing, counting and printing part: I don't know what to do anymore honestly! Been learning java for 3 months now and it's the first time I'm stuck!

    Counting Occurences from Inputted Integers HELP!-trial-algorithm.jpg

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Counting Occurences from Inputted Integers HELP!

    You forgot something. Do it with pen and paper and pay attention to what you are actually doing. Your three steps are not detailed enough to be converted into code.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Counting Occurences from Inputted Integers HELP!

    1. Access the elements of the array by creating a for loop.
    2. Compare the elements of the array with each other to find a match.
    3. Create counter.
    4. If there is a match, increment the counter.
    5. Counter must reset and save number of counts
    5. Continue comparing
    6. If there is another unique match, increment the counter again.
    7. Counter mus reset and save number of counts
    8. Ends when all array elements are compared with each other
    9. Access array by creating a for loop.
    10. Single-out unique elements.
    11. Print out the unique elements.
    12. Print out their occurrences.

    Is what I'm trying to do.

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Counting Occurences from Inputted Integers HELP!

    Ok, what I would try to do is the following:
    Java Code:
    1) create array as big as the original one
    2) loop over original array 
      2.1) if the current number is not in new array insert number of original into new array
    3) new array only cointains each individual number once
    4) loop over new array
      4.1) loop over original
        4.2) increment counter each time the numbers match
      4.3) display number and counter
      4.4) reset counter for next number
    5) You're done
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Counting Occurences from Inputted Integers HELP!

    Thank you!

    So far I've come up with this based on your algorithm. I know what I want to do as you've already told me but I can't seem to get the result.

    Java Code:
    import java.util.*;
    public class CaseStudy1{
        public static void main (String [] reyben){
            Scanner scan = new Scanner(System.in);
            int numinput = 0, counter = 1;
            
            //ask for array range
            System.out.println("How many numbers do you like to input?: ");
            numinput = scan.nextInt();
        
                //only 20 numbers max inputs
                if (numinput>20){
                System.out.println("Maximum is 20 numbers only!");
                }
                else {
                    
                    //below 20, ask for inputs and store in array
                    System.out.println("Enter " + numinput + " numbers: ");    
                    int numbers[] = new int [numinput];
                    for (int i = 0; i<numbers.length; i++){
                        numbers[i] = scan.nextInt();
                    }
                
                    //new array    
                    int newArray[] = new int [numbers.length];
                    
                    // loop original array and store unique numbers in new array
                    for (int i=0; i<numbers.length; i++){
                        if (numbers[i]!= newArray[i]){
                            newArray[i] = numbers[i];    
                        } 
                        
                        //loop over original, increment counter when there is match, print out, then reset counter
                        counter = 1;
                        for (i = 0; i<newArray.length; i++){
                            for (int j=0; j<numbers.length; j++){
                                if (newArray[i]==numbers[i]){
                                    counter++;
                                }
                            System.out.println(numbers[i] + "\t\t\t" + counter);    
                            }
                        }
                    
                    }
                
                }        
        }
    }
    Last edited by ProgrammingNoob; 09-27-2013 at 02:42 PM.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Counting Occurences from Inputted Integers HELP!

    don't attach the code or post a picture, just post the code itself here and use code tags to pretty-format it. The advanced post editor has a button to do that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Counting Occurences from Inputted Integers HELP!

    1) Where does the for loop beginning in line 28 end?
    2) Check line 29: does this really test whether the number isn't anywhere in newArray?
    3) Where is point 4.4 from my pseudocode?
    4) What happens if the user enters a letter instead of a number?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Counting Occurences from Inputted Integers HELP!

    Quote Originally Posted by PhHein View Post
    4) What happens if the user enters a letter instead of a number?
    Or 0, or -1000. Both valid integers and not higher than 20.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How to check duplicate Strings inputted
    By javazers in forum New To Java
    Replies: 9
    Last Post: 09-02-2012, 02:30 PM
  2. Ordering Inputted Java Values
    By MichaelSpame in forum New To Java
    Replies: 2
    Last Post: 07-16-2012, 05:05 AM
  3. Replies: 1
    Last Post: 10-08-2011, 05:41 AM
  4. How to count occurences of charectors in string?
    By satheeshtech in forum New To Java
    Replies: 3
    Last Post: 01-06-2010, 10:31 AM
  5. Help Creating A Graph From Inputted Data
    By adlb1300 in forum New To Java
    Replies: 6
    Last Post: 10-28-2007, 04:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •