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