[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Java - I am trying to count how many occurrences happen with

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 9
Thread images: 1

Java - I am trying to count how many occurrences happen with integers in an array that I add inputs with Scanner and the program ends when the user inputs 0. The input should be something like this:
4 occurs 3 times
42 occurs 2 times
50 occurs 5 times
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int[] numbers = new int[100 + 1];
int number = 0;
int count = 0;

System.out.print("Enter integers between 1 and 100: ");
while ((number = input.nextInt()) != 0) {
numbers[number]++;
}

for (int m = 0; m < numbers.length; m++) {
for (int n = 0; n < numbers.length; n++) {
if (numbers[m] == numbers[n]) {
count++;
}

}
if (count > 1) {
System.out.println(numbers[m] + " occurs " + count + " times");
} else {
System.out.println(numbers[m] + " occurs " + count + " time");
}
}

}
>>
This is what I have currently, trying to figure out. It is just simply outputting the numbers from 1 to 100 and checking but it should just check the numbers I am inputting instead
>>
>>57325015
If you have limited range for the numbers, for example 1 - 100, then just allocate an array of 100 elements, setup it zero and increment the indexed elements when they are encountered in the scanner loop, and then just output the elements that are greater than 0. Otherwise use hash array.
>>
Learn c
>>
>not using ternary statements
kill yourself
>>
>>57325183
first of all, this

>>57325178
but this is your solution
>>
>>57325178
Oh wait, you just sort of did that. Just remove the count++ loop and output only those elements that numbers[m] > 0.
>>
>>57325015

Use a map where the keys are the ints and the values are the occurences. Bascially you jsut ask "you have this key?", if not you add it. If it's there you raise it.

But watch out, you have to cast the ints to INTEGER, otherwise you will reference to the index, not the actual keys:

if (null != my_hashMap.get( (Integer) i) ) {
// raise by one here
} else {
// create new entry
}


At the end you just grab every entry in your map and print the result:

public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.print(pair.getKey() + " occurs " + pair.getValue() + " time");
if (pair.getValue() > 1) {
System.out.println("s");
} else {
System.out.println();
}

it.remove(); // avoids a ConcurrentModificationException
}
}
>>
>array of 100+1
>skipping numbers[0] entirely
start learning to count from zero. Do something like numbers[--number]++. What people see on the outside should ever rarely be what you code on the inside unless it is clear and efficient.
Thread posts: 9
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.