dinakar gorti
dinakar gorti

Reputation: 1

Cannot find symbol exception

import java.util.Scanner;
class Question1{
    void test(String s, int d){

        Scanner in = new Scanner(System.in);

        System.out.println("Enter a String");
        s = in.nextLine();
        System.out.println("Enter an Integer");
        d = in.nextInt();

        String c="";

        for(int i=0;i<d;i++){
             c = c + s;
        }
    }
}
class Times{
     public static void main(String args[]){

          Question1 q= new Question1();
          q.test(c);
     }
}

This is my entire code and it is showing compile time Error : Cannot find symbol c. I have searched regarding it and gone through the code but was not able to fix it.

Upvotes: 0

Views: 1040

Answers (2)

Venkat
Venkat

Reputation: 2579

Consider your Times Class :

public static void main(String args[]){

          Question1 q= new Question1();
          q.test(c);
     }

q.test(c);

First thing error there is no variable or object c in your class Times , So it shows cannot find a symbol.

Secondly the method test will take two arguments , it should be called like

test(String,int)

If you want to get the value of c in Times Class make the return type of test method to string and assign it to a variable in Times Class

Upvotes: 0

cstr_
cstr_

Reputation: 98

The object c only exists within the scope of your function test(). Calling test() with only one parameter would also result in an error, as your function signature requires two arguments (one of type int and one of type String), which is not provided.

Given that you overwrite the values that would be passed into your function, it would be better to remove the arguments altogether and instead declare d and s as local variables inside the function.

Upvotes: 1

Related Questions