Results 1 to 11 of 11
 09272013, 12:16 PM #1Member
 Join Date
 Sep 2013
 Posts
 5
 Rep Power
 0
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:
Last edited by ProgrammingNoob; 09272013 at 12:19 PM.
 09272013, 12:38 PM #2Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,094
 Rep Power
 8
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
 09272013, 01:01 PM #3Member
 Join Date
 Sep 2013
 Posts
 5
 Rep Power
 0
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!
 09272013, 01:04 PM #4Member
 Join Date
 Sep 2013
 Posts
 5
 Rep Power
 0
 09272013, 01:05 PM #5
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 1800[(10x)(13i)^2][sin(xy)/2.362x]
The Ubiquitous Newbie Tips
 09272013, 01:13 PM #6Member
 Join Date
 Sep 2013
 Posts
 5
 Rep Power
 0
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. Singleout unique elements.
11. Print out the unique elements.
12. Print out their occurrences.
Is what I'm trying to do.
 09272013, 01:30 PM #7
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 1800[(10x)(13i)^2][sin(xy)/2.362x]
The Ubiquitous Newbie Tips
 09272013, 02:06 PM #8Member
 Join Date
 Sep 2013
 Posts
 5
 Rep Power
 0
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; 09272013 at 02:42 PM.
 09272013, 02:08 PM #9Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,094
 Rep Power
 8
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 prettyformat it. The advanced post editor has a button to do that.
"Syntactic sugar causes cancer of the semicolon."  Alan Perlis
 09272013, 02:43 PM #10
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 1800[(10x)(13i)^2][sin(xy)/2.362x]
The Ubiquitous Newbie Tips
 09272013, 02:59 PM #11Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,094
 Rep Power
 8
Similar Threads

How to check duplicate Strings inputted
By javazers in forum New To JavaReplies: 9Last Post: 09022012, 02:30 PM 
Ordering Inputted Java Values
By MichaelSpame in forum New To JavaReplies: 2Last Post: 07162012, 05:05 AM 
string replaceall occurences with strings inside arrays
By DeCoolJB in forum New To JavaReplies: 1Last Post: 10082011, 05:41 AM 
How to count occurences of charectors in string?
By satheeshtech in forum New To JavaReplies: 3Last Post: 01062010, 11:31 AM 
Help Creating A Graph From Inputted Data
By adlb1300 in forum New To JavaReplies: 6Last Post: 10282007, 05:45 AM
Bookmarks