Results 1 to 15 of 15
- 03-31-2010, 07:48 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Doubt about Vertical Histogram with a for loops
Hi everyone,
I am writing a program which reads all the characters of a text file and determines how many times each letter of the English alphabet appears in the text file. Afterwards, the program should print a vertical histogram showing the 26 English letters frequencies. The name of the text file should be provided as a command line argument when you run the program.
Here is what I have done by now:
Here is an example of a file to read:Java Code:import java.util.Scanner; import java.io.*; public class Lab10b { public static void main(String[] args) throws IOException { Scanner fileScan; String line; char currentChar; int [] array = new int [26]; int max = 0, currentValue; fileScan = new Scanner(new File(args[0])); while (fileScan.hasNext()) { line = fileScan.nextLine(); line = line.toLowerCase(); for(int counter=0; counter < line.length(); counter = counter +1) { currentChar = line.charAt(counter); if (currentChar == 'a') array[0]++; else if (currentChar == 'b') array[1]++; else if (currentChar == 'c') array[2]++; else if (currentChar == 'd') array[3]++; else if (currentChar == 'e') array[4]++; else if (currentChar == 'f') array[5]++; else if (currentChar == 'g') array[6]++; else if (currentChar == 'h') array[7]++; else if (currentChar == 'i') array[8]++; else if (currentChar == 'j') array[9]++; else if (currentChar == 'k') array[10]++; else if (currentChar == 'l') array[11]++; else if (currentChar == 'm') array[12]++; else if (currentChar == 'n') array[13]++; else if (currentChar == 'o') array[14]++; else if (currentChar == 'p') array[15]++; else if (currentChar == 'q') array[16]++; else if (currentChar == 'r') array[17]++; else if (currentChar == 's') array[18]++; else if (currentChar == 't') array[19]++; else if (currentChar == 'u') array[20]++; else if (currentChar == 'v') array[21]++; else if (currentChar == 'w') array[22]++; else if (currentChar == 'x') array[23]++; else if (currentChar == 'y') array[24]++; else if (currentChar == 'z') array[25]++; } } for(int i = 0; i <= 25; i++) { if (array[i] > max) max = array[i]; } currentValue = max; for(int i = 0; i <= max; i++) { for(int j = 0; j <= 25; j++) { if(array[j] <= currentValue) System.out.print(" "); else System.out.print("*"); } System.out.println(); currentValue--; } System.out.println("abcdefghijklmnopqrstuvwxyz"); } }
Friends are God's way of taking care of us.
Friendship doubles your joys, and divides your sorrows.
A friend is someone who walks in when the whole world has walked out.
Here is the Output:
My issue is that I am getting a blank line in the first line of the output. I can't find the problem causing it, I guess is in the for loop. Hope you can help me out.Java Code:[COLOR="Red"][B]<----This BLANK Line[/B][/COLOR] * * * * * * * * * * * ** * ** * ** * * ** * ** * ** ** * * ** * ** ** * * ** ** ** ** * * *** ** * ** ** * * * *** ** * ** ** * * * * *** ** ** ** **** * * * ****** ** ** **** * * **************** ****** * abcdefghijklmnopqrstuvwxyz
Thanks!Last edited by georgio777; 03-31-2010 at 08:33 AM.
- 03-31-2010, 08:55 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 03-31-2010, 09:08 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
What's the deal with
I am doing something wrong?Java Code:if(array[j] <= currentValue)
- 03-31-2010, 09:13 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You have more of letter o than anything else in your sample text -- 15 of them. The o element is array[14]. So what will this code print when j = 14 and currentValue = 15? What do you want it to print?
-Gary-Java Code:if(array[j] <= currentValue) System.out.print(" "); else System.out.print("*");
- 03-31-2010, 09:22 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Oh my god, now I get it, how dummy I am!
Thanks for the help!
- 03-31-2010, 09:25 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Please consider posting your final code, and marking the thread SOLVED.
-Gary-
- 03-31-2010, 09:37 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Here is the final code:
In red are the changes from the original.
Java Code:import java.util.Scanner; import java.io.*; public class Lab10b { public static void main(String[] args) throws IOException { Scanner fileScan; String line; char currentChar; int [] array = new int [26]; int max = 0, currentValue; fileScan = new Scanner(new File(args[0])); while (fileScan.hasNext()) { line = fileScan.nextLine(); line = line.toLowerCase(); for(int counter=0; counter < line.length(); counter = counter +1) { currentChar = line.charAt(counter); if (currentChar == 'a') array[0]++; else if (currentChar == 'b') array[1]++; else if (currentChar == 'c') array[2]++; else if (currentChar == 'd') array[3]++; else if (currentChar == 'e') array[4]++; else if (currentChar == 'f') array[5]++; else if (currentChar == 'g') array[6]++; else if (currentChar == 'h') array[7]++; else if (currentChar == 'i') array[8]++; else if (currentChar == 'j') array[9]++; else if (currentChar == 'k') array[10]++; else if (currentChar == 'l') array[11]++; else if (currentChar == 'm') array[12]++; else if (currentChar == 'n') array[13]++; else if (currentChar == 'o') array[14]++; else if (currentChar == 'p') array[15]++; else if (currentChar == 'q') array[16]++; else if (currentChar == 'r') array[17]++; else if (currentChar == 's') array[18]++; else if (currentChar == 't') array[19]++; else if (currentChar == 'u') array[20]++; else if (currentChar == 'v') array[21]++; else if (currentChar == 'w') array[22]++; else if (currentChar == 'x') array[23]++; else if (currentChar == 'y') array[24]++; else if (currentChar == 'z') array[25]++; } } for(int i = 0; i <= 25; i++) { if (array[i] > max) max = array[i]; } [COLOR="red"][B]max--;[/B][/COLOR] currentValue = max; for(int i = 0; i <= max; i++) { for(int j = 0; j <= 25; j++) { if(array[j] <= currentValue) System.out.print(" "); else System.out.print("*"); } System.out.println(); currentValue--; } System.out.println("abcdefghijklmnopqrstuvwxyz"); } }
- 03-31-2010, 09:40 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Yikes! No, that's not it. I'm glad you posted it. Take a look again at the line I called your attention to. Don't be confused by the array index and the value stored at that index.
-Gary-
- 03-31-2010, 09:43 AM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Also, did you read what I wrote about char arithmetic?
-Gary-
- 03-31-2010, 09:53 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
- 03-31-2010, 09:56 AM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Yes. Can you tell me why?
Also, it's a completely separate issue, but do you understand what I meant about char arithmetic?
-Gary-
- 03-31-2010, 09:56 AM #12
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
- 03-31-2010, 10:03 AM #13
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Well if I change the line
When j = 14 and currentValue = 15 its going to print the asterisk instead of the space.Java Code:if(array[j] < currentValue)
I don't really get what you meant with char arithmetic, I related to what you said about getting rid of the if, if else statements.Last edited by georgio777; 03-31-2010 at 10:05 AM.
- 03-31-2010, 10:07 AM #14
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Also, with the new changes I realized that the output is still wrong:
A new line appeared...Java Code:* * * * * * * * * * * ** * ** * ** * * ** * ** * ** ** * * ** * ** ** * * ** ** ** ** * * *** ** * ** ** * * * *** ** * ** ** * * * * *** ** ** ** **** * * * ****** ** ** **** * * **************** ****** * [B][COLOR="Red"]**************************[/COLOR][/B] abcdefghijklmnopqrstuvwxyz
The solution I gave before works, the blank line has disappeared and the output is correct.Last edited by georgio777; 03-31-2010 at 10:10 AM.
- 03-31-2010, 05:30 PM #15
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
It "works" because you've gotten yourself hopelessly confused, and your errors happen to cancel each other out. You're computing max correctly, then decrementing it for no reason. Then you're counting up in a loop that should be counting down, which forces you to use a superfluous currentValue variable, and you are doing one too many iterations in that loop. Finally, you are using a wrong comparison which, combined with the extra loop iteration reverses the off-by-one error you introduced by decrementing max.
I'm going to hate myself for doing this, but here is the correct code.
Java Code:for(int i = max; i > 0; i--) { for(int j = 0; j < 26; j++) { if(array[j] < i) System.out.print(" "); else System.out.print("*"); } System.out.println(); } System.out.println("abcdefghijklmnopqrstuvwxyz");
Similar Threads
-
simple histogram
By heartysnowy in forum New To JavaReplies: 2Last Post: 03-07-2010, 07:34 AM -
histogram equalization
By syarizma in forum New To JavaReplies: 2Last Post: 08-14-2009, 03:03 AM -
Regarding the display of histogram
By Mazharul in forum Java 2DReplies: 2Last Post: 08-27-2008, 03:09 AM -
Arrays and Histogram Help Needed
By sebbybey in forum New To JavaReplies: 4Last Post: 08-15-2008, 09:01 PM -
How to create a Histogram
By Devilsfutbol17 in forum New To JavaReplies: 4Last Post: 06-04-2008, 09:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks