Through the following code i look to club all the alphabets with starting with the same name into one array column and in the second column i want to keep the sum of the numbers associated with the alphabets.
For example :
array_1 = { {"bat","1"},
{"rat","2"},
{"ball","3"},
{"run","4"},
{"lizard","5"}
}
into array_2 = { {"b","4"},
{"r","6"},
{"l",5}
}
The following code gives half correct results. The problem when it reaches ball it again adds the next alphabet starting with b and stores it as a separate value. the problem is line number 42.I have marked that.How should i impose a check that it doesn’t add the number of the alphabet once it has added.
package keylogger;
import java.util.Arrays;
public class ArrayTester {
private static int finalLength=0;
private static String firstAlphabet[][];
private String data[][] = {
{"Nokia" , "7"},
{"Blackberry" , "1"},
{"Nimbus","10"},
{"Apple","19"},
{"Ami","21"},
{"Bug","35"},
{"Native","200"},
{"zebra","100"},
{"Nine","9"}
};
public void calculator() {
try {
// traverse the whole array
firstAlphabet = new String[data.length][data.length]; // set the length of firstAlphabet array
for(int i=0;i<data.length;i++) {
firstAlphabet[i][0] = data[i][0].substring( 0, 1); // get the first alphabet
firstAlphabet[i][1] = data[i][1];
int k = i+1;
int v = k;
int t=0;
for(int j=0;j<data.length-v;j++) {
System.out.println("Inner for loop" + j);
String s = data[k][0];
// line 42:
if(firstAlphabet[i][0].compareToIgnoreCase(s.substring(0, 1))==0) {
System.out.println("If statement");
firstAlphabet[i][0] = s.substring(0, 1);
Integer z = Integer.parseInt(data[k][1]) + Integer.parseInt(firstAlphabet[i][1]);
firstAlphabet[i][1] = z.toString();
}
k++;
}
}
}catch(Exception exc) {
exc.printStackTrace();
}
}
public static void main(String args[]) {
ArrayTester o = new ArrayTester();
o.calculator();
for(String s[] : firstAlphabet) {
for(String x : s) {
System.out.println(x);
}
}
}
}
OUTPUT
Inner for loop0
Inner for loop1
If statement
Inner for loop2
Inner for loop3
Inner for loop4
Inner for loop5
If statement
Inner for loop6
Inner for loop7
If statement
Inner for loop0
Inner for loop1
Inner for loop2
Inner for loop3
If statement
Inner for loop4
Inner for loop5
Inner for loop6
Inner for loop0
Inner for loop1
Inner for loop2
Inner for loop3
If statement
Inner for loop4
Inner for loop5
If statement
Inner for loop0
If statement
Inner for loop1
Inner for loop2
Inner for loop3
Inner for loop4
Inner for loop0
Inner for loop1
Inner for loop2
Inner for loop3
Inner for loop0
Inner for loop1
Inner for loop2
Inner for loop0
Inner for loop1
If statement
Inner for loop0
N
226
null
null
null
null
null
null
null
B
36
null
null
null
null
null
null
null
N
219
null
null
null
null
null
null
null
A
40
null
null
null
null
null
null
null
A
21
null
null
null
null
null
null
null
B
35
null
null
null
null
null
null
null
N
209
null
null
null
null
null
null
null
z
100
null
null
null
null
null
null
null
N
9
null
null
null
null
null
null
null
If we notice the sum associated with the alphabets is correct. The only problem is repetition. i.e for example N=219 which is 200+19
Repetition is caused because you dont mark already selected Alphabet as dirty.
So in the first loop itself N has got a final count but with your current logic when Nimbus comes under itertion, you will do the entire processing since you havent marked N as dirty.