The following code creates a string array 'one' as [c, a, t, a, n, d, d, o, g]. Now I want to create an int array 'two' in which place of every 'a' is 3 and all other places are filled by 5 forming
int two= {5, 3, 5, 3, 5, 5, 5, 5, 5}
but the code is giving every element equal to 5, so it prints as
5 5 5 5 5 5 5 5 5 :
The code I used starts here:
import com.google.common.collect.ObjectArrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
public class StringInt {
public static void main(String[] args) {
String str= "cat and dog";
String[] word = str.split("\\s");
String[] one = new String[0];
for(int i=0; i<word.length; i++){
one = ArrayUtils.addAll(one, word[i].split("(?!^)"));
}
System.out.println("One : " + Arrays.toString(one));
int[] b = new int[one.length];
for(int j=0; j<one.length; j++){
if(one[j]=="a"){
b[j]=3;
} else {
b[j]=5;
}
System.out.print(b[j]+" ");
}
}
}
Being new to programming and java I need help to rectify this code to get the required output:
5 3 5 3 5 5 5 5 5