forked from VihaanVerma89/javaCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency.java
More file actions
39 lines (35 loc) · 1.1 KB
/
frequency.java
File metadata and controls
39 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Given an input FILE consisting of numbers separated by spaces, write a program which displays
the frequency distribution for the given input.
*/
public class frequency
{
public static void main(String [] args)
{
Map<Integer, Integer> numberMap = new TreeMap<Integer, Integer>();
Scanner scanner;
try
{
scanner = new Scanner(new File("numbers"));
while(scanner.hasNextInt())
{
int n = scanner.nextInt();
if(numberMap.containsKey(n))
{
int value = (int) numberMap.get(n);
numberMap.put(n, ++value);
}
else
{
numberMap.put(n, new Integer(1));
}
}
for(Map.Entry<Integer,Integer> entry : numberMap.entrySet())
{
System.out.println(entry.getKey() + " appeared " + entry.getValue()+ " times.");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}