Results 1 to 15 of 15
  1. #1
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Question 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:

    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");			
    	}
    }
    Here is an example of a file to read:

    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:
    Java Code:
                              [COLOR="Red"][B]<----This BLANK Line[/B][/COLOR]
                  *           
                  *           
        *         *   *       
        *         *   *       
        *         *   *       
       **         *  **       
    *  **   *     *  **       
    *  **   *    **  **   *   
    *  **   *    **  **   *   
    *  **  **    **  **   *   
    *  *** **  * **  ** * *   
    *  *** **  * **  ** * * * 
    *  *** ** ** **  **** * * 
    *  ****** ** **  **** * * 
    **************** ****** * 
    abcdefghijklmnopqrstuvwxyz
    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.

    Thanks!
    Last edited by georgio777; 03-31-2010 at 09:33 AM.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by georgio777 View Post
    Java Code:
    				if(array[j] <= currentValue)
    					System.out.print(" ");
    				else
    					System.out.print("*");
    Look carefully at that first line.

    Also, you can do math on char values, and you can leverage that to get rid of that whole block of if..else if. (hint: array[currentChar - 'a']++)

    -Gary-

  3. #3
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    What's the deal with

    Java Code:
    if(array[j] <= currentValue)
    I am doing something wrong?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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?
    Java Code:
    				if(array[j] <= currentValue)
    					System.out.print(" ");
    				else
    					System.out.print("*");
    -Gary-

  5. #5
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Oh my god, now I get it, how dummy I am!

    Thanks for the help!

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Please consider posting your final code, and marking the thread SOLVED.

    -Gary-

  7. #7
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    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");			
    	}
    }

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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-

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Also, did you read what I wrote about char arithmetic?

    -Gary-

  10. #10
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    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-
    You are suggesting to change the line to
    Java Code:
    if(array[j] < currentValue)

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Yes. Can you tell me why?

    Also, it's a completely separate issue, but do you understand what I meant about char arithmetic?

    -Gary-

  12. #12
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Also, did you read what I wrote about char arithmetic?

    -Gary-
    Yeah, I read that, but the issue is that I am taking Java classes and the teacher is grading based on what we know by now. Anyway I am going to take it into consideration.

  13. #13
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Yes. Can you tell me why?

    Also, it's a completely separate issue, but do you understand what I meant about char arithmetic?

    -Gary-
    Well if I change the line
    Java Code:
    if(array[j] < currentValue)
    When j = 14 and currentValue = 15 its going to print the asterisk instead of the space.

    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 11:05 AM.

  14. #14
    georgio777 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Also, with the new changes I realized that the output is still wrong:

    Java Code:
                  *           
                  *           
        *         *   *       
        *         *   *       
        *         *   *       
       **         *  **       
    *  **   *     *  **       
    *  **   *    **  **   *   
    *  **   *    **  **   *   
    *  **  **    **  **   *   
    *  *** **  * **  ** * *   
    *  *** **  * **  ** * * * 
    *  *** ** ** **  **** * * 
    *  ****** ** **  **** * * 
    **************** ****** * 
    [B][COLOR="Red"]**************************[/COLOR][/B]
    abcdefghijklmnopqrstuvwxyz
    A new line appeared...

    The solution I gave before works, the blank line has disappeared and the output is correct.
    Last edited by georgio777; 03-31-2010 at 11:10 AM.

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by georgio777 View Post
    The solution I gave before works, the blank line has disappeared and the output is correct.
    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

  1. simple histogram
    By heartysnowy in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 08:34 AM
  2. histogram equalization
    By syarizma in forum New To Java
    Replies: 2
    Last Post: 08-14-2009, 04:03 AM
  3. Regarding the display of histogram
    By Mazharul in forum Java 2D
    Replies: 2
    Last Post: 08-27-2008, 04:09 AM
  4. Arrays and Histogram Help Needed
    By sebbybey in forum New To Java
    Replies: 4
    Last Post: 08-15-2008, 10:01 PM
  5. How to create a Histogram
    By Devilsfutbol17 in forum New To Java
    Replies: 4
    Last Post: 06-04-2008, 10:22 PM

Posting Permissions

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