Skip to content
Advertisement

How can I get a value from an array [closed]

I am trying to solve this problem, I want to use these numbers one by one, but I cannot.

String a = "101";
char[] array = a.toCharArray();
for (int i = 0; i < array.length; i++) {
    // if i print array[i] it says 1 but in below,
    // it will give a reference. i want to get 1
    if (array[i] == 1) {
        System.out.print(1);
    } else {
        System.out.print(0);
    }

Advertisement

Answer

Try this :

    String a="101";
    char [] array=a.toCharArray();
    for(int i=0; i<array.length;i++) {
          if(array[i]== '1'){
            System.out.println(1);
        }else {
            System.out.println(0);
        }
    }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement