473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I print the occurrence of the number once?

my code asks the user for a number N
then it asks the user again for N numbers
it sorts the numbers ascending and print's it with the number of occurrences.

prints:
How many integers should I collect? 5
1: 2
2: 4
3: 4
4: 5
5: 6

2: 1
4: 1
4: 1
5: 1
6: 1

how I want it to print:
How many integers should I collect? 5
1: 2
2: 4
3: 4
4: 5
5: 6

2: 1
3: 0
4: 2
5: 1
6: 1


Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Histogram{
  3.     public static void main(String args[]){ 
  4.         Scanner input = new Scanner(System.in); 
  5.  
  6.         int count = 0;
  7.  
  8.         System.out.print("How many integers should I collect? ");
  9.         int[] integers = new int[input.nextInt()]; 
  10.  
  11.         count++;
  12.  
  13.         for (int i = 0; i < integers.length; i++) { 
  14.             System.out.print( + (i + 1) + ": ");
  15.             integers[i] = input.nextInt(); 
  16.  
  17.         }
  18.         java.util.Arrays.sort(integers); 
  19.         System.out.println("");
  20.         for(int i = 0; i < integers.length; i++) { 
  21.             System.out.println(+ integers[i] + ": " + count); 
  22.  
  23.  
  24.     }
  25.     }
Apr 28 '13 #1
1 1245
Rabbit
12,516 Expert Mod 8TB
You only incremented your count once. What you need to do is insert or increment a new array based on whether or not it exists already.
Apr 29 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bhanu | last post by:
I have a problem in a piece of C Program. I have to print a number of size 19 digit (No decimals). What data type should I use to store and print this no. I have already tried using double, long...
8
by: cyberdwarf | last post by:
Hi I need to print a report which will always output an even number of pages (this is for duplex printing). ie, if the report ends with an odd number of pages, I need to output a blank sheet... ...
3
by: Orendavd | last post by:
hi there, I have a working UDP server in c++, how to I print the IP of the received packet???
5
by: dmitrey | last post by:
hi all, could you inform how to print binary number? I.e. something like print '%b' % my_number it would be nice would it print exactly 8 binary digits (0-1, with possible start from 0) ...
0
by: castironpi | last post by:
On May 7, 3:31 pm, Mensanator <mensana...@aol.comwrote: ) for a in range( 10 ) ] ) 00000000 00000001 00000010 00000011 00000100 00000101 00000110
1
by: mfaisalwarraich | last post by:
Hi All, I want to print the data in specified number of columns on a web page. for example i wana show only 7 columns on a page and i also want empty column between that column to seperate each...
4
by: paragguptaiiita | last post by:
Hi I am new in Perl, i want to print the week number from the Date. I fetched date from localtime() function, now how can we convert this date into the week number? Thanks in advace :)
1
by: Sin Jeong-hun | last post by:
It looked like that I can print a number in hexadecimal format with Console.Write, but I couldn't find how to print in binary format. I mean 000...0001 for 1, 000...0010 for 2 and so on. I wrote...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.