Search code examples
javavariablesbufferedreaderinputstreamreader

cannot be resolved to a variable in a for loop in a loop


i cannot seem to get the bottom "i" to link to a variable in the for loop below where have i gone wrong?? i have tried to edit it change the variable and put the variable above the for loop all i get is error

also i am using eclipse luna

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AssignmentProgramming {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);

    System.out.println("Please enter a string");


    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String data = reader.readLine();


    char ch =(char) System.in.read();

    int letters=0;

    for(int i=0; i<data.length(); i++);
    {
        char c=data.charAt(i);//<-- This i here
        if (c==ch);
        {letters++;
    }



      }
    System.out.println(letters);
    }

    }

Solution

  • remove semicolon ; at the end of your for loop

    for(int i=0; i<data.length(); i++);
                                      ^
    

    and at ifstatement

    if (c==ch);
              ^