Results 1 to 3 of 3
Thread: Array count number Occurances
- 04-17-2009, 12:53 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
Array count number Occurances
I am working on some code which is supposed to count the number of occurences of each digit between 1 and 50. printing the results as follows:
Number 1: 2
Number 2: 0
Number 3: 1
....
It should do this by storing the digit in an array to tally the number of occurances of each digit.
I have tried to do this by looping over the string input by the user then looping over the array to add each character to its correct place.
i have not managed to achieve this does anyone know how i have gone wrong
Java Code:package chapter7projects; import java.util.Scanner; public class PP71 { public static void main (String[] args) { final int MAX = 50; final int MIN = 0; int count = 0; Scanner scan = new Scanner (System.in); int[] numArray = new int[MAX]; int current; // the current character being processed System.out.println ("Enter a number:"); String line = scan.nextLine(); for(int i = 0; i <= line.length(); i++) //loop round input string { for(int n = 0; n <= MAX; n++) //loop round array tallying num occurances { if(numArray[i] == n) { numArray[i] = count+1; } } } for (int j = 0; j < numArray.length; j++) { System.out.println("Nunber " + (j + 1) + ": " + numArray[j]); } } }-Long time no c-
-:eek:-
- 04-17-2009, 06:56 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
this is one of the times where i'd like to ask where YOU think it's going wrong. what have you done to debug it? you say you "have not managed to achieve this", but you don't mention any insight into what you think is erroneous. your best bets right now would be to rethink the following pieces of code:
Java Code:String line = scan.nextLine(); for(int i = 0; i <= line.length(); i++) //loop round input stringJava Code:for(int n = 0; n <= MAX; n++) //loop round array tallying num occurances { if(numArray[i] == n) { numArray[i] = count+1; } }
- 04-17-2009, 08:34 PM #3
Hint, hint...
A couple of hints...
You are comparing n against what's in the array, but.... when did any data get put into the array?Java Code:for(int n = 0; n <= MAX; n++) //loop round array tallying num occurances { if(numArray[i] == n) { numArray[i] = count+1; } }
In the above code, you have the user's input in a sting (line)... when is this string used on further comparations?Java Code:String line = scan.nextLine();
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
How to count the number of specific object in a Vector collection?
By johnsienk in forum New To JavaReplies: 4Last Post: 03-28-2009, 04:58 PM -
Number Array to Sound
By Phantasmagorical in forum Advanced JavaReplies: 8Last Post: 03-01-2009, 05:36 PM -
[SOLVED] How to count the number of words in a string
By andy5605 in forum New To JavaReplies: 8Last Post: 02-04-2009, 08:55 PM -
[SOLVED] Array of first negative number
By random0munky in forum New To JavaReplies: 9Last Post: 12-08-2008, 11:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks