Reputation: 107
If
char [] a = {'x','y','x'};
I need an
int [] b={5,3,5};
It is clear that 'x' correspond to 5 and 'y' corresponds to 3.
I tried to get this way through JAVA code:
public static void main(String[] args) {
char [] a= {'x','y','x'};
int[] b ={};
for(int i=0; i<a.length; i++){
if( a[i]=='x'){
b[i]=3;
} else {
b[i]=5;
}
System.out.println(b[i]);
}
}}
but failed. I need help.
Upvotes: 0
Views: 38
Reputation: 62864
Define the b
array like:
int[] b = new int[a.length];
And also, since you want x
to correspond to 5
, you have to do:
if(a[i] == 'x') {
b[i] = 5;
} else {
b[i] = 3;
}
Upvotes: 2