public int numTimes(int numToFind, int[] array)
{
int count = 0;
for(int i : array)
{
if(i == numToFind)
{
count++;
}
}
return count;
}
> I am kind of new to java, just to be sure will that
> method find the amount of times numbers in an array
The best way to be sure of anything would be to take the method, put in code, compile it and run it and see what happens.
Please note you are going to have to make an effort to complete your own homework assignment. CaptainMorgan gave you a good start but it looks from your last post that you still have some work to do.
Here is what I have so far, I can check for the amount but when I get to the other number in the array it will check again and I'll have two outputs of the same number.
public class Amount{
public static void main (String [] args){
double[] abc = {6, 3, 35, 353, 55 , 6};
int count = 0;
for(int i = 0; abc[i] < abc.length; i++){
if(i > 0){
for(int a = 0; abc[a] < abc.length; a++){
if(abc[i] == abc[a]{
break;
}
}
break;
}else{
for(int b = 0; abc[b] < abc.length; b++){
if(abc[i] == abc[b]){
count++;
}
}
}
System.out.println("The Number " + abc[i] + " appears " + count + "times");
}
}
}
public int numTimes(int numToFind, int[] array) {
String s = Arrays.toString(array);
String[] tokens = s.split("[^" + numToFind +"]+");
return tokens.length - 1;
Since we're posting solutions. However, someone will need to check my regex as it gives one too many tokens.
I'm trying to find the amount of times a number appears in a ten number array set by the user. I can check the numbers but I'll get too many outputs because it checks checks for every number so if it check for 6 and there are two of them it will also check that 2 as well so I get two outputs saying "There are 2 2s" when I only need one
> I have no idea what a map is, I am new to java
Getting started with Java
[url=http://java.sun.com/docs/books/tutorial/ ]Sun's basic Java tutorial[/url]
[url=http://java.sun.com/learning/new2java/index.html ]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.
[url=http://javaalmanac.com ]http://javaalmanac.com [/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance ]The Java Developers Almanac[/url].
> It has to automatically do all numbers.
Your example only includes ints. So it may include an array of double? The reason I ask, is that checking equality in doubles can be somewhat trickier than for ints.
Again, if there is low chance that you will be dealing with doubles (or floats), keep the array an array of ints.
I have tried and came up with this:
public class Amount{
public static void main (String [] args){
int[] abc = {6, 3, 35, 353, 55 , 6, 4, 23, 6, 8, 10};
String[] def = new String[10];
int count = 0;
for(int i = 0; abc[i] < abc.length; i++){
for(int b = 0; abc[b] < abc.length; b++){
if(abc[i] == abc[b]){
count++;
}
}
def[i] = ("The Number " + abc[i] + " appears " + count + "times");
}
int d = 0;
for(int q = 0; q < def.length; q++){
if(def[q].equals(def[d])){
def[q] = def[d];
}
d++;
}
for(int w = 0; w < def.length; w++){
System.out.println(def[w]);
}
}
}
The problem is I get a null pointer exception. I try to put it in strings and then check the strings against each other.
Ooh, problems here:
> for(int i = 0; abc[i] < abc.length; i++){
and here
> for(int b = 0; abc[b] < abc.length; b++){
needs to be something like
for(int i = 0; i < abc.length; i++){
and
for(int b = 0; b < abc.length; b++){
Message was edited by:
petes1234
yeah, thanks for pointing that out, that was a huge problem, I changed it a little
public class Amount{
public static void main (String [] args){
int[] abc = {6, 3, 35, 353, 55 , 6, 4, 23, 6, 8, 10};
String[] def = new String[10];
int count = 0;
for(int i = 0; i < abc.length; i++){
for(int b = 0; b < abc.length; b++){
if(abc[i] == abc[b]){
count++;
}
}
def[i] = ("The Number " + abc[i] + " appears " + count + "times");
}
int d = 1;
for(int q = 0; q < def.length; q++){
if(def[q].equals(def[d])){
def[q] = def[d];
}else{
def[q] = def[q];
}
d++;
}
for(int w = 0; w < def.length; w++){
System.out.println(def[w]);
}
}
}
EDIT: says out of bounds at line 14 and I don't see why
Message was edited by:
seithex
The actual error message said something about "ArrayIndexOutOfBounds" and it refered you to this line:def[i] = ("The Number " + abc[i] + " appears " + count + "times");
In general you get this exception when (a) the index i is negative or (b) the index i is too big for the array.
Now i is never negative, so it's a case of the array index getting too big.
How big can i get? (Ie, what is the largest value i ever has?) Do the two array expressions - def[i] and abc[i] - make sense for this value of i?
Here is my most recent copy:
public class Amount{
public static void main (String [] args){
int[] abc = {6, 3, 35, 353, 55 , 6, 4, 23, 6, 8, 10};
String[] def = new String[10];
int count = 0;
for(int i = 0; i < abc.length; i++){
for(int b = 0; b < abc.length; b++){
if(abc[i] == abc[b]){
count++;
}
}
def[i] = ("The Number " + abc[i] + " appears " + count + "times");
}
int d = 1;
for(int q = 0; q < def.length; q++){
if(d == 10){
break;
}
else if(def[q].equals(def[d])){
def[q] = def[d];
}else{
def[q] = def[q];
}
d++;
}
for(int w = 0; w < def.length; w++){
System.out.println(def[w]);
}
}
}
I still get the out of bounds, but it shouldn't be out of bounds
else if(def[q].equals(def[d])){
def[q] = def[d];
}else{
def[q] = def[q];
What is the point of this? If def[q] equals def[d] you will simply be copying the same value over itself. Same for def[q] = def[q]. Totally pointless.
One other piece of advice. You can get quick fixes here in the forum, but as a general rule, you're best sweating out most of this coding and debugging alone. I suggest that if you have a specific question that desperately needs answering, you ask it here in the forum, but then you close your internet browser and work, study, change, play, and otherwise torment your code for a good bit of time, at least 12 hours or more.
In other words: You have to work most of this out yourself in order for you to fully understand and appreciate it. Plus you'll get that sense of satisfaction when something finally goes right because of your own intelligence and perseverence. It's the same feeling that hooked most of us into coding in the first place.