0

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.

1
  • How did you fail? Why does your solution not work as intended? Commented May 23, 2014 at 15:16

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have seen your detailed solution on the above link. I think it is very valuable for newbies like me.
@user3660819 Don't hesitate to use this website to share sample with your futures questions :).
Yes, I will never hesitate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.