1 Replies - 237 Views - Last Post: 09 October 2012 - 03:23 PM Rate Topic: -----

#1 FrozenDinosaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-September 12

Error code! I can not figure out how to fix it

Posted 09 October 2012 - 03:12 PM

The code I'm currently trying to make is suppose to read the number of characters within the string and display it in a vertical histogram. For example
         *           
              *           
    *         *   *       
    *         *   *       
    *         *   *       
   **         *  **       
*  **   *     *  **       
*  **   *    **  **   *   
*  **   *    **  **   *   
*  **  **    **  **   *   
*  *** **  * **  ** * *   
*  *** **  * **  ** * * * 
*  *** ** ** **  **** * * 
*  ****** ** **  **** * * 
**************** ****** * 
abcdefghijklmnopqrstuvwxyz


However i end up getting this error code and I cant see what I'm doing wrong
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at histo1.main(histo1.java:13)



This is my code
import java.util.Scanner;
import java.io.*;
 
public class graph
{
    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");           
    }
}

I know i can shorten this code, but I'm just really stump on this error I keep getting. Please and thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Error code! I can not figure out how to fix it

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Error code! I can not figure out how to fix it

Posted 09 October 2012 - 03:23 PM

There probably isn't any parameters being passed to the main method, so args[] doesnt have any elements in it. Try passing a specific path on line 13:
fileScan = new Scanner(new File("C:\\file.txt"));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1