Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" problem?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" problem?

    I have this program to see the occurrences of a character in a string stored in a text file.
    My text file has the following contents (as shown below):
    P-P-N-N-L-V
    N-R-U-S-R-Q
    K-Q-E-L-E-A
    A-J-B-G-E-F
    F-E-L-Q-R-R
    U-F-J-V-I-M
    P-I-Q-K-B-K
    U-N-L-F-O-B
    M-A-N-M-H-O
    P-Q-J-D-N-I
    G-U-O-D-F-I
    Q-M-M-B-C-Q
    N-B-I-L-E-J
    S-A-T-Q-H-U
    C-L-G-U-J-M
    R-P-V-M-A-A
    C-R-A-V-L-B
    M-U-Q-K-M-Q
    E-I-C-H-V-J
    J-N-N-K-R-P


    As you notice, each character is seperated by a hypen (serving as its delimiter). I want to only go through 10 lines in the text, instead of all 20 of them. So, I wrote the program in such a way that I intend to reach into the indices of each character in the text file. Since there are six characters in each line so that means there are 6 indices - and there are 10 lines I only want to go through. So, 6 indices times 10 lines equals 60 indices. Hence, there are only 60 indices I need to go through. In that manner, it's like I have gone through only 10 lines through that way.
    It compiled perfectly fine but upon running it, I ran through an error in the black DOS screen that says "java.lang.ArrayIndexOutofBoundsException: 6 ".
    How do I work around that?

    The code I wrote is shown below...
    import java.io.*;
    import java.util.*;
     
    public class hotandcoldclusterdeterminer_test {
    	public static void main (String args [])throws IOException {
    		Scanner search = new Scanner (new File ("string.txt"));
    		Scanner record = new Scanner (new File ("output.txt"));
     
    		int counterA=0;
    		int counterB=0;
    		int counterC=0;				
     
    		String counterrecordA=" ";		
    		String counterrecordB=" ";		
    		String counterrecordC=" ";				
    		String spacer="--------------------------------------------------------------";
     
    		while (search.hasNextLine())
    		     { //while loop starting brace
    			  String scanline = search.nextLine();
    			  String charArray[]  = scanline.split("-");
    			  for (int counter=0; counter<=10; counter++)
    			     { // for loop starting brace
    			      if (charArray[counter].equals("A"))
    			        {  //first main outer if loop starting brace
    				     counterA++;
    				     counterrecordA="A has occurred " + counterA + " times \t\n";
    			        }  //first main outer if loop ending brace
    			 else if (charArray[counter].equals("B"))
    			        {  //second main outer if loop starting brace
    				     counterB++;
    				     counterrecordB="B has occurred " + counterB + " times \t\n";
    			        }  //second main outer if loop ending brace
    			 else if (charArray[counter].equals("C"))
    			        {  //third main outer if loop starting brace
    				     counterC++;
    				     counterrecordC="C has occurred " + counterC + " times \t\n";
    			        }  //third main outer if loop ending brace			        
            	     } // for loop ending brace
    		     }//while loop ending brace
     
    		FileWriter recorder=new FileWriter("output.txt",true);
    		recorder.write(spacer+System.getProperty("line.separator"));						
    		recorder.write(counterrecordA+System.getProperty("line.separator"));
    		recorder.write(spacer+System.getProperty("line.separator"));						
    		recorder.write(counterrecordB+System.getProperty("line.separator"));
    		recorder.write(spacer+System.getProperty("line.separator"));						
    		recorder.write(counterrecordC+System.getProperty("line.separator"));
    		recorder.write(spacer+System.getProperty("line.separator"));						
    		recorder.close();				        		
    	}
    }
    Last edited by PC_flea; April 26th, 2014 at 08:02 AM. Reason: edited typo errors in post


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" problem?

    What line is the error on? Where is the index given a value?
    The Normal for loop continuation condition should be for while the index is LESS than the size of the array.
    Remember array indexes range from 0 to the array length-1

    The for loop should use the .length attribute of the array to determine its length, not a magic number like 10.

    --- Update ---

    Also posted at: how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" problem?

    So if I am to use the length attribute, is this how I should write it?
    for (int charArray.length=0; charArray.length <=60;charArray.length++)

    And the loop -
    for (int counter=0; counter<=6; counter++)
    - will be inside the loop above then, is that right?
    (Like the one below?)
              for (int charArray.length=0; charArray.length <=60;charArray.length++) 
                 {
                   for (int counter=0; counter<=6; counter++)
                      { ... }
                 }

    By the way, were the syntaxes I wrote correct?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" problem?

    What happened when you tried it?

Similar Threads

  1. "Exception in thread "main" java.lang.NullPointerException" help?
    By redstripes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2014, 08:59 AM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM

Tags for this Thread