-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
67 lines (57 loc) · 1.53 KB
/
Graph.java
File metadata and controls
67 lines (57 loc) · 1.53 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package tweetAnalysis;
import java.io.FileReader;
import java.io.IOException;
public class Graph {
public static int[] read(String filename) throws IOException {
@SuppressWarnings("resource")
FileReader inputStream = new FileReader(filename);
int num = 0;
int[] letters = new int[26];
for(int i=0;i<letters.length;i++) {
letters[i] = 0;
}
while((num = inputStream.read()) != -1) {
if(num>=97 && num<=122) {//a to z
letters[num-97]++;
}else if(num>=65 && num<=90) {//A to Z
letters[num-65]++;
}
}
return letters;
}
public static void graph(int[] letterFreqArray) {
double max = maxVal(letterFreqArray);
char myChar = 'A';
double newVal = 0;
String lines = "";
double[] doubleArray = new double[letterFreqArray.length];
for(int i=0;i<letterFreqArray.length;i++){
newVal = letterFreqArray[i]/max;
doubleArray[i] = (int) (newVal*80);
myChar = (char)(i+65);
for(int j=0;j<doubleArray[i];j++){
lines+="|";
}
if(letterFreqArray[i]!=0 && lines.length()!=0){
System.out.println(myChar +" "+ lines+" ("+letterFreqArray[i]+")");
}else if(lines.length()==0){
System.out.println(myChar + " ("+letterFreqArray[i]+")");
}else{
System.out.println(myChar +" (0)");
}
lines = "";
}
}
private static int maxVal(int[] array){
int currMax = array[0];
for(int i=1;i<array.length;i++){
if(array[i]>currMax){
currMax = array[i];
}
}
return currMax;
}
public static void main(String[] args) throws IOException {
graph(read("tweets.txt"));
}
}