-1

what's the error her :(

انا بحاجه الي حل هذا السؤال فكرتة في طباعه الحروف المتكررة في جملة يدخلها المستخدم

public class Checker {
        Scanner Scan = new Scanner(System.in);
        int x;
        public Checker()
        {
            System.out.print("Enter The Word ; ");
            String Word = Scan.nextLine();
            System.out.print("Enter The character : ");
            char Char = Scan.next().charAt(0);
        }

        for( int  i = 0 ; i < Word.lenght ; i++ )
        {
             char Ch = Word.charAt(i);
             if(Ch == Char )
                  x++ ;
        }
         System.out.println(x);

    }
Mahmoud Alaa
  • 3
  • 1
  • 4

2 Answers2

1

You would need to wrap all the code beyond constructor in a method like main/check for e.g. like:

Scanner Scan = new Scanner(System.in);
    int x;
    String Word;
    char Char;
    public Checker()
    {
        System.out.print("Enter The Word ; ");
        Word = Scan.nextLine();
        System.out.print("Enter The character : ");
        Char = Scan.next().charAt(0);
    }

 public void check() {
    for( int  i = 0 ; i < Word.lenght ; i++ )
    {
         char Ch = Word.charAt(i);
         if(Ch == Char )
              x++ ;
    }
     System.out.println(x);
 }
 public static void main(String args[]) {//lets use Checker now to check
    Checker c = new Checker();
    c.check();
 }
SMA
  • 36,381
  • 8
  • 49
  • 73
0

I dont understand your question either; from looking at your code; one thing that is missing (but shouldnt matter): "x" is not initialized in your code.

GhostCat
  • 137,827
  • 25
  • 176
  • 248