1 Replies - 792 Views - Last Post: 26 April 2014 - 07:42 AM Rate Topic: -----

#1 code_flea   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-April 14

how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" pr

Posted 26 April 2014 - 06:07 AM

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):

Quote

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();				        		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" pr

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: how do I fix a "java.lang.ArrayIndexOutofBoundsException:6" pr

Posted 26 April 2014 - 07:42 AM

Your for loop is looping over the characters found in a single line
There are not 10 characters in a line, so the lookup in the array goes out of bounds

Instead of looping over 10 lines, you are instead trying to loop over 10 characters in a line
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1