Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 16th, 2007, 01:08 PM
Authorized User
 
Join Date: May 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default ArrayIndexOutOfBoundsException

Why does it give me this error and how can I fix it?


Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:0
             at Alphabet.main(Alphabet.jaba:9)


import java.io.*;
import java.util.*;

public class Alphabet
{
    public static void main(String args[] )
        throws IOException
    {
        BufferedReader in =
            new BufferedReader(
               new FileReader(args[0]));
        String delims =
            "How to program java";
        int totA = 0, totB = 0, totC = 0, totD = 0, totE = 0, totF = 0,
            totG = 0, totH = 0, totI = 0, totJ = 0, totK = 0, totL = 0,
            totM = 0, totN = 0, totO = 0,totP = 0, totQ = 0, totR = 0,
            totS = 0, totT = 0, totU = 0,totV = 0, totW = 0, totX = 0,
            totY = 0, totZ = 0;

        // Read lines:
        String line;
        while ((line = in.readLine()) != null)
        {
            // Tokenize line:
            StringTokenizer strtok =
                new StringTokenizer(line, delims, true);
            while (strtok.hasMoreTokens())
            {
                char vowel = strtok.nextToken().charAt(0);
                switch(Character.toLowerCase(vowel))
                {
                case 'a':
                    ++totA;
                    break;
                case 'b':
                    ++totB;
                    break;
                case 'c':
                    ++totC;
                    break;
                case 'd':
                    ++totD;
                    break;
                case 'e':
                    ++totE;
                    break;
                case 'f':
                    ++totF;
                    break;
                case 'g':
                    ++totG;
                    break;
                case 'h':
                    ++totH;
                    break;
                case 'i':
                    ++totI;
                    break;
                case 'j':
                    ++totJ;
                    break;
                case 'k':
                    ++totK;
                    break;
                case 'l':
                    ++totL;
                    break;
                case 'm':
                    ++totM;
                    break;
                case 'n':
                    ++totN;
                    break;
                case 'o':
                    ++totO;
                    break;
                case 'p':
                    ++totP;
                    break;
                case 'q':
                    ++totQ;
                    break;
                case 'r':
                    ++totR;
                    break;
                case 's':
                    ++totS;
                    break;
                case 't':
                    ++totT;
                    break;
                case 'u':
                    ++totU;
                    break;
                case 'v':
                    ++totV;
                    break;
                case 'w':
                    ++totW;
                    break;
                case 'x':
                    ++totX;
                    break;
                case 'y':
                    ++totY;
                    break;
                case 'z':
                    ++totZ;
                    break;


                }//end switch

            }//end while


        }//end while

        System.out.printf("%s%8s\n", "Character", "Total");//column headings

        System.out.println("'A's: " + totA);
        System.out.println("'B's: " + totB);
        System.out.println("'C's: " + totC);
        System.out.println("'D's: " + totD);
        System.out.println("'E's: " + totE);
        System.out.println("'F's: " + totF);
        System.out.println("'G's: " + totG);
        System.out.println("'H's: " + totH);
        System.out.println("'I's: " + totI);
        System.out.println("''J's: " + totJ);
        System.out.println("'K's: " + totK);
        System.out.println("'L's: " + totL);
        System.out.println("'M's: " + totM);
        System.out.println("'N's: " + totN);
        System.out.println("'O's: " + totO);
        System.out.println("'P's: " + totP);
        System.out.println("'Q's: " + totQ);
        System.out.println("'R's: " + totR);
        System.out.println("'S's: " + totS);
        System.out.println("'T's: " + totT);
        System.out.println("'U's: " + totU);
        System.out.println("'V's: " + totV);
        System.out.println("'W's : " + totW);
        System.out.println("'X's: " + totX);
        System.out.println("'Y's: " + totY);
        System.out.println("'Z's: " + totZ);

    }//end main
}//end class


 
Old July 16th, 2007, 01:19 PM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Appears that you are trying to read the parameter from commandline which is not provided.
new FileReader(args[0]) This tries to get the first param passed to the program. If it is not present, then it will throw up the exception. Make sure you pass the file name to the java program as a parameter.

-eNJay
 
Old July 16th, 2007, 02:08 PM
Authorized User
 
Join Date: May 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How do i do this?

 
Old July 17th, 2007, 01:25 AM
Authorized User
 
Join Date: Oct 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should have some txt file (for exapmple). As said eNJay above you should pass file name as a parameter.
You can do it in two ways.
1. Use following:

BufferedReader in =
            new BufferedReader(new FileReader ("test.txt"));

text.txt is your file name.

2. Or you should give your file name as your arg[0]. Put file name while calling -java after classname: java Alphabet test.txt
// this way is better cause you can have different files for which you can use the same program

And one more thing. System.out.printf("%s%8s\n", "Character", "Total");//column headings
Guess this one is from C ;)

Best,
Anna





Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.ArrayIndexOutOfBoundsException chiraagb Pro JSP 1 July 1st, 2006 07:21 PM
Strange ArrayIndexOutOfBoundsException ISsue kapweb J2EE 0 January 7th, 2006 07:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.