aspose file tools*
The moose likes Beginning Java and the fly likes count occurrence of number in array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "count occurrence of number in array" Watch "count occurrence of number in array" New topic
Author

count occurrence of number in array

Roxanne Billings
Greenhorn

Joined: Apr 01, 2011
Posts: 8
I am stuck . . .please help!

My assignment has the following criteria:

Using a linear search determine how many of each number was input . . .

I have already created the array and limited the values to numbers ranging from 1-6, but the only method in my book I see is an example of counting the occurrence of each letter in an array, but I am not sure how to modify it to count numbers.

The code to count the letters is:



It seems like this would work similarly for numbers, but not sure how to modify to make it produce that result. I hope someone can help me; I would sincerely appreciate the assistance!

RB
javascript:emoticon('');
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
I won't tell you that if you go here and search for "Map interface" you will find a counting example using a Map. It isn't there, unless you look very closely.

The simpler method of incrementing an array element would work nicely too, as you already have. Have you found the Character class, or whether there are any methods giving a numerical value to a char? I won't tell you about that either.
Roxanne Billings
Greenhorn

Joined: Apr 01, 2011
Posts: 8
Thank you for the help, and please excuse the fact that I am very early beginner with java so I am still confused. I looked the the link that you "didn't really tell me about" and thought about your other comments, but I am still lost! Can you elaborate a little further?

Here is the code I have on my assignment to this point (if that helps to see where I am going):



Again, I sincerely appreciate your help and time!

RB
javascript:emoticon('');
Roxanne Billings
Greenhorn

Joined: Apr 01, 2011
Posts: 8
I have made some progress, but now I am receiving an error when I run the file. Can someone PLEASE help me?

Here is my code:



and here is the error that I get when I run the file:

run:
2
6
5
6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -49
3
1
6
5
3
1
2
6
1
2
5
3
2
3
6
6
3
4
2
3
2
2
3
3
1
at assignment6_main.Assignment6_main.countNumbers(Assignment6_main.java:38)
at assignment6_main.Assignment6_main.main(Assignment6_main.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Look at line 38 in your last post...

Note that inside single quotes, '1' is a char. And the char '1' has an int value of 49, which probably explains your error message...

java.lang.ArrayIndexOutOfBoundsException: -49


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Roxanne Billings
Greenhorn

Joined: Apr 01, 2011
Posts: 8
ok -- I see what you are saying, but how would I state the "1" value without it being the char type I used?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: count occurrence of number in array