Results 1 to 10 of 10

Thread: ArrayIndexOutOfBoundsException

  1. #1
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException

    The console keeps printing out :

    "init:
    deps-jar:
    Compiling 1 source file to C:\proj\build\classes
    compile:
    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at proj.Main.main(Main.java:28)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    "

    whenever I compile....

    I dont know how the fix the error.. please help....

    Code:
    package proj;
    
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import proj.Store;
    
    /**
     *
     * @author gmanr26
     */
    public class Main {
        
        public Main() {
        }
        
        public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
            Scanner stuff = new Scanner (new File(args[0]));
            if (stuff.next()=="NEWCUST"){
                String cust_num = stuff.next();
                stuff.next();
                String dob = stuff.next() + "/" + stuff.next() + "/" + stuff.next();
                stuff.next();
                String sex = stuff.next();
                stuff.next();
                String name = null;
                while (stuff.next()!= "HOME#") {
                    name = name + " " + stuff.next(); 
                }
                String home_num = stuff.next();
                stuff.next();
                String cell_num = stuff.next();
                stuff.next();
                String work_num = stuff.next();
                stuff.next();
                String email = stuff.next();
                stuff.next();
                Integer ln = Integer.parseInt(stuff.next());
                String addr= null;
                while (ln >0){
                    addr = addr + " " + stuff.nextLine();
                }
                
            }
        }
        
    }
    "And what's the real lesson? Don't leave things in the fridge"

  2. #2
    Join Date
    May 2004
    Posts
    168
    Rep Power
    0

    Default

    This execption is indicating that you are trying to read an index that is not available. I think the problem lies with the next() function.

    You program appears to be reading a formatted file and looking for certain 'Key words' in order to assign variables. I do not beleive this is the most efficient method, but check the following

    1) Ensure that the "CASE" in your file is the same as in the Program
    2) Check your file to ensure that all of the check points exists
    NEWCUST,HOME#

    Looking at the code it appears that once "NEWCUST" is located there should be at least 8 tokens before reaching your while loop.

    Basically you need to look for anything that will cause your program to pass one on the checks you have in place.

    Post a copy of your file that you are using, that contains the data. That will assist in locating the prob easier.

  3. #3
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default

    here is an excerpt from the file.....
    I haven't coded for the all the commands yet

    "NEWCUST 1977 DOB 07 17 1887 GENDER M TITLE Hon NAME Marcus Mosiah Garvey
    HOME# 927-7777
    CELL# N/A
    WORK# 977-7777
    EMAIL N/A
    ADDR 2
    1 Lady Musgrave Ave.
    Kingston 5 Jamaica
    OPEN SAVINGSACCT 5676 CUST 1977 OPENED 12 02 2006 BAL 500.00
    GETBAL 5676"
    "And what's the real lesson? Don't leave things in the fridge"

  4. #4
    Join Date
    Sep 2003
    Posts
    603
    Rep Power
    0

    Default

    I am not a Java Programmer and I might be totally incorrect but the language is close enough to C++ and C# for me to make an educated guess.

    you cant execute this application standalone, you need to pass some command line variables/arguements eg "myapp.exe NEWCUST". you should be able to specify command line arguements in the IDE while running the application in debug mode.

    the only place in your code that can produce an array index exception is the line which reads "Scanner stuff = new Scanner (new File(args[0]));" (right after entry into main). you are trying to execute the "File" constructor with the first item in the "args" array which might not exist.

    modify your code with this.....
    Code:
    package proj;
    
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import proj.Store;
    
    /**
     *
     * @author gmanr26
     */
    public class Main {
        
        public Main() {
        }
        
        public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
    if (args.length > 0) {
            Scanner stuff = new Scanner (new File(args[0]));
            if (stuff.next()=="NEWCUST"){
                String cust_num = stuff.next();
                stuff.next();
                String dob = stuff.next() + "/" + stuff.next() + "/" + stuff.next();
                stuff.next();
                String sex = stuff.next();
                stuff.next();
                String name = null;
                while (stuff.next()!= "HOME#") {
                    name = name + " " + stuff.next(); 
                }
                String home_num = stuff.next();
                stuff.next();
                String cell_num = stuff.next();
                stuff.next();
                String work_num = stuff.next();
                stuff.next();
                String email = stuff.next();
                stuff.next();
                Integer ln = Integer.parseInt(stuff.next());
                String addr= null;
                while (ln >0){
                    addr = addr + " " + stuff.nextLine();
                }
                
            }
        }
       }
    }
    easiparcel.com Shop online and ship to Jamaica

  5. #5
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default

    i edited the code u said to
    and here is the new problem coming out in the comsole

    "init:
    deps-jar:
    Compiling 1 source file to C:\proj\build\classes
    C:\proj\src\proj\Main.java:18: cannot find symbol
    symbol : method length()
    location: class java.lang.String[]
    if (args.length()>0){
    1 error
    BUILD FAILED (total time: 0 seconds)
    "
    "And what's the real lesson? Don't leave things in the fridge"

  6. #6
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Code:
    if (args.length()>0){
    length is an attribute not a method, remove the method call parentheses

    Code:
    if ( args.length > 0 ) {
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  7. #7
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default

    Thanks mint.... ur a savior
    "And what's the real lesson? Don't leave things in the fridge"

  8. #8
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default

    Code:
    import proj.Store;
    prob here
    in the line above I'm importing from a class i made in the package... but it cant see any of my public methods.... help please

    here is the code for that class


    Code:
    /*
     * Store.java
     *
     * Created on December 17, 2006, 11:50 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package proj;
    
    import java.lang.*;
    
    /**
     *
     * @author gmanr26
     */
    public class Store {
        
        public String [][] cust_info = new String [9][1000];
        public String [][] acc_info = new String [5][1000];
        private int count = 0;
        private int counter = 0; 
        
        public void deposit (String acc_num, String dollars){
           int spot;
           spot = search_array_2 (acc_num, acc_info);
           if (spot >=0){
               String bal = acc_info [5][spot];
               Integer bal_int = (Integer.parseInt(bal));
               Integer dep = Integer.parseInt(dollars);
               bal_int = bal_int + dep;
               bal = bal_int.toString();
               acc_info [5][spot] = bal;
           }       
        }
        public void withdraw (String acc_num, String dollars){
            int spot;
            spot = search_array_2(acc_num, acc_info);
            if (spot>=0){
                String bal = acc_info [5][spot];
                Integer bal_int = Integer.parseInt(bal);
                Integer with = Integer.parseInt(dollars);
                if (bal_int>= with ){
                    bal_int = bal_int - with;
                    bal = bal_int.toString();
                    acc_info [5][spot] = bal;
                }            
            } 
        }
        public String get_bal (String acc_num){
            String bal = null;
            int spot;
            spot = search_array_2(acc_num, acc_info);
            if (spot>=0){
                bal = acc_info [5][spot];
            }
            return bal;
        }
        public void new_acc (String [] args){
            //if (args.length>0)
            for (int a = 0; a<5; a++){
                acc_info [a][counter] = args [a];
            }
            counter++;
        }
        public void end_acc (String acc_num){
            int spot;
            spot = search_array_2 (acc_num, acc_info);
            if (spot >=0){
               
            }
        }
        private int search_array_2 (String ac_num, String [][] from){
            int line= 0;
            String answer = null;
            for (int a = 0; a <1000; a++){
                if (from [2][a] == ac_num) {
                    line = a;
                }
                else {
                    line = -1;
                }
            }
            return line;
        } 
        private String [][] clear_array (String [][] full){
            for (int a =0; a < 1000; a++){
                for (int b = 0; b < 9; b++){
                    full [b][a]= null;
                }
            }
            return full;
        }
        private String [][] copy_array (String [][] from, String [][] to,
                                        int begin_1, int end_1, int begin_2, 
                                        int end_2){
            clear_array (from);
            for (int a = begin_1; a < end_1; a++){
                for (int b = 0; b <9; b++){
                    to [b][a] = from [b][a];
                }
            }
            for (int a = begin_2; a < end_2; a++){
                for (int b = 0; b <9; b++){
                    to [b][(end_1 - 1)] = from [b][a];
                    end_1++;
                }
            }
            return to;
        }
        private String print_cust_line (int line, String [][] from){
            String ln = null;
            for (int a = 0; a < 9; a++){
                ln = ln + " " + from [a][line];
            } 
            return ln;
        }
        private String search_array (String ac_num, String [][] from){
            int line;
            String answer = null;
            for (int a = 0; a <1000; a++){
                if (from [1][a] == ac_num) {
                    line = a;
                    answer = print_cust_line (line, from);
                }
                else {
                    answer = "This customer does not exist";
                }
            }
            return answer;
        }
        private void delete (int ln){
            String [][]temp = new String [9][1000];
            copy_array (cust_info, temp, 0, ln-1, ln+1, 1000);
            clear_array (cust_info);
            copy_array (temp, cust_info, 0, 0, 0, 1000);
            clear_array (temp);
            count--;
        }
        public void new_cust (String [] args){
           // if (args.length>0){
                for (int a = 0; a < 9; a++){
                cust_info [a][count] = args [a]; 
                }
                count++;
           // }    
        }    
    }
    "And what's the real lesson? Don't leave things in the fridge"

  9. #9
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by gmanr26 View Post
    Code:
    import proj.Store;
    prob here
    in the line above I'm importing from a class i made in the package... but it cant see any of my public methods.... help please
    importing the class only makes the class available in another package, if they are in the same package (as would most likely be the case for an assignment) you dont have to import the class.


    Code:
    Store inst = new Store();
    inst.deposit("xargon1","598635.07")
    ...
    Code:
    deposit("xargon1","598635.07"); // <- this will not work
    now, are you saying when you create an instance of the class you cant see any public methods on that instance, or are you mixing up the syntax of java with some other language.
    Last edited by icymint3; Dec 18, 2006 at 03:59 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  10. #10
    Join Date
    Sep 2003
    Posts
    603
    Rep Power
    0

    Default

    This is just a general comment;

    I have been looking at your code and I want to give you a word of advice. stop using cryptic variable names, rename your variables to be more descriptive. if you go for an interview as an entry level programmer with no experience, chances are you employer is going to want to see some of your code fragments.

    your code:
    Code:
            for (int a = begin_1; a < end_1; a++){
                for (int b = 0; b <9; b++){
                    to [b][a] = from [b][a];
                }
    Improved:
    Code:
            for (int outerIndex = begin_1; outerIndex < end_1; outerIndex ++){
                for (int innerIndex = 0; innerIndex  <9; innerIndex ++){
                    to [innerIndex ][outerIndex] = from [innerIndex][outerIndex];
                }
    its always a terrrible idea to use "a", "b", "myVar" and those other things as variable names.... just a tip.... happy programming
    easiparcel.com Shop online and ship to Jamaica

Posting Permissions

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