error in comparison using substring

2
maquina += numeroale.nextInt(4);
        maquina += numeroale.nextInt(4);

        //vez da mqauina
    for (int i = 0; i < maquina.length(); i++) {

        System.out.println("valor atual"+maquina.substring(i,1));

        if((maquina.substring(i,1)).equals("0")){
            System.out.println("0 pressed");
            b1.setBackground(Color.white);




            play("b1");




        }
        else if((maquina.substring(i,1)).equals("1")){
            System.out.println("1 pressed");
            b2.setBackground(Color.white);

            play("b2");

        }
        else if((maquina.substring(i,1)).equals("2")){
            System.out.println("2 pressed");
            b3.setBackground(Color.white);


            play("b3");



        } else if((maquina.substring(i,1)).equals("3")){
            System.out.println("3 pressed");
            b4.setBackground(Color.white);


        play("b4");

        }
        else{

        }



    }   

The code can not compare to the second position, but the first one goes after the error

    
asked by anonymous 23.05.2016 / 05:08

1 answer

1

The problem is that the second parameter ( endIndex ) of substring is unique .

It also indicates the position where it should end, not the number of characters as it is done in other languages.

This means that endIndex must be higher (at least 1 more) than beginIndex . Give your code fails in the second iteration where the value of i is equal to 1.

Please correct for the following, everywhere:

maquina.substring(i, i + 1)
    
23.05.2016 / 12:36