# Thread: Problem with a Simple Histogram Problem

1. Member
Join Date
May 2013
Posts
18
Rep Power
0

## 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.IOException;
import java.util.ArrayList;

import acm.io.*;
import acm.program.*;
import acm.util.*;

public class Histogram extends ConsoleProgram {

public void run() {
try	{
while (true){
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. ## 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. Member
Join Date
May 2013
Posts
18
Rep Power
0

## 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.

4. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,701
Rep Power
4

## 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

5. Member
Join Date
May 2013
Posts
18
Rep Power
0

## Re: Problem with a Simple Histogram Problem

Originally Posted by jim829
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. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,701
Rep Power
4

## 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 06:33 AM.

7. Member
Join Date
May 2013
Posts
18
Rep Power
0

## 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.

8. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,701
Rep Power
4

## 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

9. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,701
Rep Power
4

## 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

10. ## 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. Member
Join Date
May 2013
Posts
18
Rep Power
0

## Re: Problem with a Simple Histogram Problem

Originally Posted by jim829
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. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,701
Rep Power
4

## 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

13. Member
Join Date
May 2013
Posts
18
Rep Power
0

## Re: Problem with a Simple Histogram Problem

Originally Posted by jim829
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.

#### Posting Permissions

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