Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: BufferReader, histogram

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default BufferReader, histogram

    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
    Last edited by Nhedro; February 22nd, 2012 at 08:21 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: BufferReader, histogram

    there were too many words to the array.
    That's interesting that there were more than 1000 words?
    Can you explain the logic of your program? I don't see any comments that tell us what the code is trying to do.


    Try debugging your code by printing out the words that you separate from the String to see why there are so many of them.

    The code you have posted is missing some variable definitions. Where is palavras defined? What is it used for?

    Another way to get the words from a String is to use the Scanner class's next() method. Create a Scanner object with the String and use the next() method.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: BufferReader, histogram

    It was supposed to be words instead of palavras, when i do the insert.split() i dont define the length of words, and it gives an error of OutOfBonds,

    String [] word;
     
    words = insert.split(" " , 1000);

    with i separate the string whenever there is a " ", but sometimes apear ' " ' and it prints the string "The, for example, and the last String of the array is the rest of insert, containing a lot of words yet.


    And when i run without the spilt(), i get only the first word 1000 times. I forgot to say, i put 1000 as length, because it is given to me a max of 1000 diferent words.
    Last edited by Nhedro; February 22nd, 2012 at 08:34 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: BufferReader, histogram

    Work on the problem of separating the words in a small test program by itself.
    Define a String with one of the lines from the file and then work out the logic for getting the words from the String. When it works for one String, change the String and try it again. When you are using the spit() method, Use the Arrays class's toString method to format the array for printing.

    Have you tried the Scanner class for getting the words from a String?

    How many different words are there in the file? Have you looked at using the ArrayList class?
    Another useful class for this project is the Map class.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Nhedro (February 22nd, 2012)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: BufferReader, histogram

    i haven't tried none of those classes, but i will try to do has u say, work line by line, it may be much usefull. u are saying that if i use "the Arrays class's toString " i will get a word only?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: BufferReader, histogram

    the Arrays class's toString method formats the whole array.
    For testing you would want to change the array size.

Similar Threads

  1. [SOLVED] BufferReader and BufferWritter
    By Nhedro in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 14th, 2012, 09:30 AM
  2. Histogram Java Program
    By djl1990 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 24th, 2011, 06:47 AM
  3. [SOLVED] difference between fileReader and bufferReader
    By shadihrr in forum Java Theory & Questions
    Replies: 6
    Last Post: June 8th, 2010, 01:26 AM
  4. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM