How to use an existing variable as a counter

My goal is to take the letter of the alphabet that the user typed and show, as many times as they type, the predecessor and successor letters of that, you know?
Look What I did:

static int v;
static String[] Alfa = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
        "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

public static void main(String[] args) {
    String letra;
    Scanner s = new Scanner(System.in);

    System.out.print("Digite uma letra: ");
    letra = s.nextLine();
    System.out.print("Quantas letras deseja mostrar?");
    v = s.nextInt();

    for(int i = 1; i < Alfa.length; i++) {
        if(letra == Alfa[i]) {
            for(int i; i <= 1;) {
                System.out.print(Alfa[i--] + " ");
            }

            System.out.print(Alfa[i]);

            for(int i; i <= v;) {
                System.out.print(" " + Alfa[i++]);
            }
        }
    }
}  

It turns out that I have to use the value of the Alpha[i] that is inside the IF in the for's, but how do I do that?

Author: Dongabi, 2016-10-15

2 answers

It might be interesting to do an error treatment in case the user enters with a valor that will exceed the size of the String, for example choose the letter Y and show 20 elements. I didn't do that because it wasn't mentioned. In general it would look like this:

public static void main(String[] args) {
    String letra;
    Scanner s = new Scanner(System.in);

    System.out.print("Digite uma letra: ");
    letra = s.nextLine();
    System.out.println(letra);
    System.out.print("Quantas letras deseja mostrar?");
    v = s.nextInt();

    for(int i = 0; i < Alfa.length; i++) {
        if(letra.equals(Alfa[i])) {
            for(int j = v; j > 0; j--) {
                System.out.print(Alfa[i-j] + " ");

            }

            System.out.print(Alfa[i]);

            for(int j = 1; j <= v; j++) {
                System.out.print(" " + Alfa[i+j]);
            }
        }
    }
}

Input example:

D
2

Output:

B C D E F
 2
Author: Andrey França, 2016-10-15 02:43:06

As I suggested in the comment:

static int v;
static String[] Alfa = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
        "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

public static void main(String[] args) {
    String letra;
    Scanner s = new Scanner(System.in);

    System.out.print("Digite uma letra: ");
    letra = s.nextLine();
    System.out.print("Quantas letras deseja mostrar?");
    v = s.nextInt();

    for(int i = 0; i < Alfa.length; i++) {
        if(letra == Alfa[i]) {
            break;
        }
    }

    for(int j = 1; j <= i; j++) {
        if (i+j < Alfa.length) {
            System.out.print(Alfa[i+j] + " ");
        }
        if (i-j >= 0) {
            System.out.print(Alfa[i-j] + " ");
        }
    }
}  
 0
Author: guijob, 2016-10-15 02:12:30