Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By dswastik
  • 1 Post By gimbal2

Thread: I keep getting ArraysOutofBoundsException error?

  1. #1
    javanovice2 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default I keep getting ArraysOutofBoundsException error?

    I have double checked this code over and over and I just can't find the problem.
    What I'm trying to do is take a file and input it into an 2D array.
    Ultimately, I should convert the array of integers to an array of characters, then print it out. The file contains a set of ASCII values.
    After printing it out, I should then create methods to manipulate the image produced.

    Using 2D arrays is a requirement for this exercise.
    I think that somehow I'm overcomplicating this and the solution is a lot more simple than I think, but I can't think of what to change.

    The error I am getting is:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
    at main.main(main.java:17)

    Java Code:
    import java.util.*;
    import java.io.*;
    public class main {
    	public static void main(String[] args)
    			throws FileNotFoundException {
    		String[][] data = new String[22][40];
    		try{
    		Scanner input = new Scanner(new File("homer.txt"));
    
    		int row = 0;
    		int col = 0;
    		 
    		String line = null;
    		while ((line = input.nextLine()) != null && row < 22){
    			StringTokenizer st = new StringTokenizer(line, " ");
    			while (st.hasMoreTokens()){
    				data[row][col] = st.nextToken();
    				col++;
    			}
    			col = 0;
    			row++;
    		}
    		input.close();
    		String[]values = line.split(",");
    		for (String readline: values){
    			int str_int = Integer.parseInt(readline);
    			
    			row++;{
    				System.out.println(str_int);
    			}	
    			System.out.println("value of file is" + data[22][40]);
    		}
    		 } catch (FileNotFoundException e){
    		        e.printStackTrace();
    
    		    } catch (IOException e){
    
    		    }
    		
    			
    		}
    }

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default Re: I keep getting ArraysOutofBoundsException error?

    If the size is 22,40 your last accessible index is 21,39

    System.out.println("value of file is" + data[22][40]); should be


    System.out.println("value of file is" + data[21][39]);
    Swastik

  3. #3
    javanovice2 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: I keep getting ArraysOutofBoundsException error?

    That doesn't change the error, though. :( It occurs when the tokens are being put into the array.
    I can only assume this is because I messed up with the increment operators somewhere, but no matter how many times I manipulate the variables, the error just occurs in a different place or doesn't change.

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default Re: I keep getting ArraysOutofBoundsException error?

    That's a different issue. But System.out.println("value of file is" + data[22][40]); is absolutely wrong. If the length is 40 you can access from 0 to 39.
    SurfMan likes this.
    Swastik

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: I keep getting ArraysOutofBoundsException error?

    You must make ABSOLUTELY SURE, that the file that you read has EXACTLY the amount of columns you expect. The StringTokenizer will go on and on for every space it sees. If the number of columns in the file are greater than the array then the program pukes. In cases like this, build in some extra security to see what the input is, and what you expect it to be.

    And PS: dswastik is right: new String[20] has indices from 0 to 19. So System.out.println("value of file is" + data[22][40]); will ALWAYS explode, regardless of what is in it.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    javanovice2 is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: I keep getting ArraysOutofBoundsException error?

    I know for sure that this is the right number of columns and rows, though. The file provides the number of rows and columns as the first two numbers in the file.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: I keep getting ArraysOutofBoundsException error?

    Quote Originally Posted by javanovice2 View Post
    I know for sure that this is the right number of columns and rows, though. The file provides the number of rows and columns as the first two numbers in the file.
    That may well be, but you don't do anything with those numbers. All your numbers are hard-coded. I still suspect ONE of the lines in the file containing more than 39 spaces.
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
    at main.main(main.java:17)
    Line 17 is the nextToken() call and 40 is the number of spaces the Tokenizer splitted.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: I keep getting ArraysOutofBoundsException error?

    Quote Originally Posted by javanovice2 View Post
    I know for sure that this is the right number of columns and rows, though.
    You know for sure that this is SUPPOSED to be the truth, but we all know for sure that it isn't the actual truth, or your code wouldn't blow up. Simple logic, and accepting that simple logic will break barriers you now have in your brain to keep looking at this problem from a logical point of view. Which is something which you have to keep doing if you want to find this problem and accept the help people are trying to give you here.

    So problem: you want only 40 columns and you are getting AT LEAST 41. The exception is proof, you can't deny it.
    Question: So where does it go wrong?
    Answer: System.outing() some of those variables you have in your code will likely tell you the why. It might be code, it might be the data. Who knows, that's what you need to figure out.

    It might help to just try and reason what could break the code. For example: what happens when the line ends with a space (something quite invisible when you look at the file).
    SurfMan likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: I keep getting ArraysOutofBoundsException error?

    Being a lazy person, I'd print the values of row and col before line #17 and sees what happens ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: I keep getting ArraysOutofBoundsException error?

    Oh look, its the five minute period where Jos is awake in the day. Hi Jos!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: I keep getting ArraysOutofBoundsException error?

    Quote Originally Posted by gimbal2 View Post
    Oh look, its the five minute period where Jos is awake in the day. Hi Jos!
    Duh, I got exhausted from removing all that 'jordan' spam and dumping the spammers in the rotating knives machine; now I have to clean up the bloody mess ...

    kind regards,

    Jos (aka the forum janitor)
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: I keep getting ArraysOutofBoundsException error?

    Your effort is highly appreciated, although you might wonder why we all still bother to keep this site alive.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: I keep getting ArraysOutofBoundsException error?

    Quote Originally Posted by gimbal2 View Post
    although you might wonder why we all still bother to keep this site alive.
    For the repartee of course.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: I keep getting ArraysOutofBoundsException error?

    I think the most important things about the array size and the exception was said already. But I want to suggest a generic approach to solve such problems:
    Play around with debuggers. The correct handling of a debugger is simply something that all developers should know. And it is quite easy and straight forward: As a beginner you can simply grab any IDE with an integrated debugger. Netbeans, eclipse, ...

    With such a debugger, you could easly step through the code and look exactly at all the values in each variable so you see exactly what is going on.

    So in your case you can step through the application and you will see how everything works.

    And another generic thing:
    Double check all type of input. And in your case the file is some kind of input for your application. So you could some logic e.g.
    while (st.hasMoreTokens() && col < 21){

    Also think about readability! Magic numbers are something that you should always avoid! Define some constants so you get something like
    while (st.hasMoreTokens() && col < NUM_COLS){

    These was the points I was thinking of right now. I hope that my comment is of any help.

    With kind regards,

    Konrad

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 06:26 PM
  2. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 05:38 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
  •