Reputation: 851
I'm making a program that finds if there is a letter pair of 2 letters within a line of text, for example, if "AA" was inputted, I would add 1 to alphabet[0][0]. When I try and input "aabbcc" I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
at Freq.processLine(Freq.java:25)
at Freq.main(Freq.java:12)
When I input "AABBCC" I get this error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:658)
at Freq.processLine(Freq.java:23)
at Freq.main(Freq.java:12)
I'm not sure why I am getting these errors. Any help would be greatly appreciated. Here is my code:
import java.util.Scanner;
public class Freq{
private static final int ROWS = 26;
private static final int COLS = 26;
private static int[] [] alphabet = new int[ROWS][COLS];
public static void main(String[] args) {
String line;
Scanner userInput = new Scanner(System.in);
while(userInput.hasNextLine()) {
line = userInput.nextLine();
processLine(line);
}
}
public static void processLine(String line) {
line.toUpperCase();
for(int i = 0; i < alphabet.length; i++) {
for(int j = 0; j < alphabet[i].length; j++) {
for (int a = 0; a < line.length(); a++) {
char firstLetter = line.charAt(a);
char secondLetter = line.charAt(a + 1);
if (firstLetter == secondLetter) {
alphabet[firstLetter - 65][secondLetter - 65] = alphabet[firstLetter - 65][secondLetter - 65] + 1;
}
}
}
}
for (int b = 0; b < alphabet.length; b++) {
for (int c = 0; c < alphabet[b].length; c++){
System.out.print(alphabet[b][c] + " ");
System.out.println();
}
}
}
}
Upvotes: 1
Views: 120
Reputation: 1
Even after correcting the errors mentioned by Frank Puffer, your code still will not get the desired result since your first two loops do not break when the last character of String line
is reached. In this way, the code goes through the string 26*26 times. So, if the string contains one "aa" pair, instead of getting a result of "1", you will get a result of "676".
Since you can fill the array without the first two loops, you do not even need to implement the corresponding break statements - you can simply take the first two loops out of the code. In addition, the display of the characters, as implemented by the last four lines of code, would not be correct, if you want the result to be displayed in a matrix format.
Besides, depending on how you define what counts as a pair of same characters, you will have different number of pairs of identical characters for the same string, if it contains any.
First scenario: you will have one result, if you allow one and the same character to be used for the construction of different pairs with other identical neighbouring characters. Thus if the string contains "aaa", the code will count two pairs of "a" - represented by the first and the second "a", as well as by the second and the third "a".
Second scenario: you will have quite another result, if you do not allow one and the same character to be used for the construction of different pairs with other identical neighbouring characters. Thus if the string contains "aaa", the code will count only one pair of "a" - represented by the first and the second "a".
So, assuming that your code correctly represents your intended outcome, your aim is to have results as per first scenario. Anyway, below are the methods for both scenarios, including a method that displays the array in a matrix format.
First scenario:
public static void processLine(String line) {
line = line.toUpperCase();
for (int a = 0; a < line.length() - 1; a++) {
char firstLetter = line.charAt(a);
char secondLetter = line.charAt(a + 1);
if (firstLetter == secondLetter) {
alphabet[firstLetter - 65][secondLetter - 65] = alphabet[firstLetter - 65][secondLetter - 65] + 1;
}
}
display(alphabet);
}
Second scenario:
public static void processLine(String line) {
line = line.toUpperCase();
int a = 0;
while (a < line.length() - 1) {
char firstLetter = line.charAt(a);
char secondLetter = line.charAt(a + 1);
if (firstLetter == secondLetter) {
alphabet[firstLetter - 65][secondLetter - 65] = alphabet[firstLetter - 65][secondLetter - 65] + 1;
a = a + 2;
} else {
a++;
}
}
display(alphabet);
}
Display results in a matrix format:
public static void display(int[][] table) {
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
System.out.print(alphabet[i][j]);
}
System.out.println();
}
}
Upvotes: 1
Reputation: 8215
The first issue is here:
line.toUpperCase();
returns a string converted to uppercase and leaves the original string unmodified. This is clear because strings in Java are immutable.
So what you need to do is:
line = line.toUpperCase();
The other issue is that line.charAt(a + 1);
is out of bounds for a = line.length() - 1
.
Upvotes: 4