Results 1 to 13 of 13
  1. #1
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default Problem with a Simple Histogram Problem

    One of the problems from the CS106a course is to create a histogram based on a series of test scores contained within a text file. I've already looked at the official solution, but I'm wondering why my first crack at it didn't work. I originally thought it was something to do with the boolean expressions that I attempted to use to bin the scores into the correct ranges, but even the bins that don't use those expression aren't correct. Can anyone tell me why it isn't working?

    Java Code:
    /*
     * File: Histogram.java
     * ------------------
     * This program will eventually count the lines, words, and characters in a given text file.
     */
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import acm.io.*;
    import acm.program.*;
    import acm.util.*;
    
    public class Histogram extends ConsoleProgram {
    	
    	
    	public void run() {
    		try	{
    			BufferedReader scores = new BufferedReader(new FileReader("midtermScores.txt"));
    			while (true){
    				String line = scores.readLine();
    				if (line == null) break;
    				if(line.length() == 1) bin0 += "*";
    				if(line.length() == 2) {
    					if(line.charAt(0) == 0) bin0 +="*";
    					if(line.charAt(0) == 1) bin1 +="*";
    					if(line.charAt(0) == 2) bin2 +="*";
    					if(line.charAt(0) == 3) bin3 +="*";
    					if(line.charAt(0) == 4) bin4 +="*";
    					if(line.charAt(0) == 5) bin5 +="*";
    					if(line.charAt(0) == 6) bin6 +="*";
    					if(line.charAt(0) == 7) bin7 +="*";
    					if(line.charAt(0) == 8) bin8 +="*";
    					if(line.charAt(0) == 9) bin9 +="*";
    				}
    				if(line.length() == 3) bin10 += "*";
    				
    			}
    			scores.close();
    	}	catch (IOException ex)	{
    			throw new ErrorException(ex);
    	}
    		println("09 - 09: " + bin0);
    		println("10 - 99: " + bin1);
    		println("20 - 99: " + bin2);
    		println("30 - 99: " + bin3);
    		println("40 - 99: " + bin4);
    		println("50 - 99: " + bin5);
    		println("60 - 99: " + bin6);
    		println("70 - 99: " + bin7);
    		println("80 - 99: " + bin8);
    		println("90 - 99: " + bin9);
    		println("    100: " + bin10);
    		
    	}
    	
    
    	
    	private String bin0 = "";
    	private String bin1 = "";
    	private String bin2 = "";
    	private String bin3 = "";
    	private String bin4 = "";
    	private String bin5 = "";
    	private String bin6 = "";
    	private String bin7 = "";
    	private String bin8 = "";
    	private String bin9 = "";
    	private String bin10 = "";
    	
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with a Simple Histogram Problem

    Does it compile? Run? Does it not behave correctly? Please give us the details of your problem as it will help us to help you.

  3. #3
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default Re: Problem with a Simple Histogram Problem

    It compiles and runs without any issue. The problem is that all of the scores are being put in the 100 bin.

    Problem with a Simple Histogram Problem-screen-shot-2013-06-19-11.44.41-am.png

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Problem with a Simple Histogram Problem

    Well, I don't know how much data you have but your test for characters is wrong. I suggest you put single quotes around the digits you are testing for.
    e.g == '9' instead of == 9.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default Re: Problem with a Simple Histogram Problem

    Quote Originally Posted by jim829 View Post
    Well, I don't know how much data you have but your test for characters is wrong. I suggest you put single quotes around the digits you are testing for.
    e.g == '9' instead of == 9.

    Regards,
    Jim
    I'm still getting the same result. Oh well.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Problem with a Simple Histogram Problem

    What does your data look like? Can you attach the file? Oh, and BTW, your println syntax is incorrect and your ranges need some reconsideration.

    Regards,
    Jim
    Last edited by jim829; 06-21-2013 at 05:33 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default Re: Problem with a Simple Histogram Problem

    The text file with the scores is attached. What's the issue with the println syntax? I don't think it's any different from the syntax I've been using up until this point(?). Thanks.
    Attached Files Attached Files

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Problem with a Simple Histogram Problem

    I forgot you are extending some class with which I am unfamiliar (ConsoleProgram). So my guess is it probably provides a method forwarder of println() which calls System.out.println().

    In any case, I had to modify my version to get it to work. And the mystery is that each line of your input data file that is only two character wide has a space appended to it. So all lines appear to be three characters wide. And you need to put single quotes around your digit comparisons.
    The char 9 is different than the char '9' (the latter is equal to 39 (in hex)). Assuming your super class works correctly, your program should now work.
    And I still think you should change your range value. 10 - 99, 20 - 99.. does that make sense?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Problem with a Simple Histogram Problem

    Upon reflection on this I believe I did you a disservice by pointing out the trailing blanks in the text file. And for that I apologize. What I should have done was have you first verify the length of the lines you are reading. That would show a line length of 3 for each one. Then if you didn't figure out why that was happening I would have recommended that you print the lines as you read them, surrounded by single quotes. That would have shown that a blank was trailing each line. This is exactly what I did (and why I requested your datafile).

    So in the future, you should attempt to determine why certain events are happening by using print statements. It is a very simple debugging tool. And I should be more thoughtful about my assistance.

    Note: Telling you to put single quotes around your digits was in my opinion, appropriate. Internal representation of characters is not something beginning programmers are familiar with.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Problem with a Simple Histogram Problem

    Or maybe call the trim method after each line of data is read. This will ensure there are no extra spaces.

  11. #11
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default Re: Problem with a Simple Histogram Problem

    Quote Originally Posted by jim829 View Post
    Upon reflection on this I believe I did you a disservice by pointing out the trailing blanks in the text file. And for that I apologize. What I should have done was have you first verify the length of the lines you are reading. That would show a line length of 3 for each one. Then if you didn't figure out why that was happening I would have recommended that you print the lines as you read them, surrounded by single quotes. That would have shown that a blank was trailing each line. This is exactly what I did (and why I requested your datafile).

    So in the future, you should attempt to determine why certain events are happening by using print statements. It is a very simple debugging tool. And I should be more thoughtful about my assistance.

    Note: Telling you to put single quotes around your digits was in my opinion, appropriate. Internal representation of characters is not something beginning programmers are familiar with.

    Regards,
    Jim
    I really appreciate you taking the time to go through this. I think the biggest problem I'm running into while attempting to learn this stuff is my own impatience. I have to keep reminding myself that there are no shortcuts and that I just have to put in the time.

    Anyway, using the trim method on the scores and putting single quotes around the values in the boolean expression got it to work. Can you explain your recommendation to change the range values?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Problem with a Simple Histogram Problem

    Well, your ranges that you display are always from some low number to 99. Normally, they would be something like 10-19, 20-29, 30-39, etc.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default Re: Problem with a Simple Histogram Problem

    Quote Originally Posted by jim829 View Post
    Well, your ranges that you display are always from some low number to 99. Normally, they would be something like 10-19, 20-29, 30-39, etc.

    Regards,
    Jim
    Gotcha. I was confused as to what you were talking about since that's something that I caught and fixed right after I posted here. Thanks again.

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  2. Simple problem
    By melovehockey in forum New To Java
    Replies: 8
    Last Post: 12-29-2010, 01:43 AM
  3. simple histogram
    By heartysnowy in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 07:34 AM
  4. I have a simple problem
    By o0oNorao0o in forum JCreator
    Replies: 20
    Last Post: 12-25-2009, 08:32 PM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •