Hi, i have an assignment where i have to do a histogram, that is i receive a text and must count the times each word in the text appears. This method receives a BufferReader and a BufferWritter, what i did so far is transform the buffer reader into a string and i tried some things already, but couldnt find a solution, and i would like to know if u have some ideas to help me.
public static void histogram( BufferedReader in, BufferedWriter out ) throws IOException{ String insert; StringBuilder sb= new StringBuilder(); String [] word= new String [1000]; int [] counter = new int [1000]; int k; while((insert=in.readLine())!=null){ sb.append(insert); sb.append("\n"); } insert = sb.toString(); // this was my first try k = 0; for(int i =0; i<insert.length(); i ++){ if(Character.isLetter(insert.charAt(i))) ; else{ for(k =0; k<words.length; k ++){ if(words [k] == insert.substring(begin , i)) counter[k]++; if(words [k] == null) words [k] = insert.substring(begin , i); } } } //this one was the second words = insert.split(" " , 1000); k = 0; for(int i =0; i<word.length; i++){ out.write(word[i] + ": " + Integer.toString(counter[i])); out.newLine(); } }
There is somethings missing that i dont know how i can do, in the first try i tried to seperate the text in words while the "for" was running the string and counting how many equal words appear; in the second i tried to seperate the full string in words and then count, but in this case there were too many words to the array.
Thank you for helping