2

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);
    }

    }
Big Moe Pasha
  • 59
  • 2
  • 3
  • 11

2 Answers2

7

remove semicolon ; at the end of your for loop

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

and at ifstatement

if (c==ch);
          ^
Baby
  • 5,062
  • 3
  • 30
  • 52
1

Your problem is to be found on line 22 of the original code. In multiple places, you have accidentally added a semicolon before the loop body, thereby missing the declaration of the variable. I have enclosed a refactored and corrected edit of your code:

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 {
        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);
    }
}

I hope this helped you, and best of luck.

Zesty Memes
  • 442
  • 2
  • 11