This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

count occurrence of number in array

 
Roxanne Billings
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roxanne Billings
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok -- I see what you are saying, but how would I state the "1" value without it being the char type I used?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic