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

    }

2 Answers 2

7

remove semicolon ; at the end of your for loop

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

and at ifstatement

if (c==ch);
          ^
Sign up to request clarification or add additional context in comments.

2 Comments

Same here: if (c==ch);
Thankyou very much for a simple solution
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.