i am trying to run this program,
class person
{
String name,city;
int age;
void set(String n,String ct,int a)
{
name=n;
city=ct;
age=a;
}
void display()
{
System.out.println("n");
System.out.println("ct");
System.out.println("a");
}
public static void main(String args[])
{
person p1= new person();
p1.setData("Dhruv","Dhule",14);
p1.display();
}
}
but the program give me compilation failed error like
Main.java:20: error: cannot find symbol
s1.setdata("dhruv","dhule",14);
^
symbol: method setdata(String,String,int)
location: variable s1 of type person
1 error
so please help me how I fix it
Answers
Answer:
Try this one, it has no error:
class person
{
String name,city;
int age;
void set(String n,String ct,int a)
{
name=n;
city=ct;
age=a;
}
void display()
{
System.out.println("n");
System.out.println("ct");
System.out.println("a");
}
public static void main(String args[])
{
person p1= new person();
p1.set("Dhruv","Dhule",14);
p1.display();
}
}
Given Códe:-
class person
{
String name,city;
int age;
void set(String n,String ct,int a)
{
name=n;
city=ct;
age=a;
}
void display()
{
System.out.println("n");
System.out.println("ct");
System.out.println("a");
}
public static void main(String args[])
{
person p1= new person();
p1.setData("Dhruv","Dhule",14);
//Here is your error
p1.display();
}
}
Corrected Códe:-
class person
{
String name,city;
int age;
void set(String n,String ct,int a)
{
name=n;
city=ct;
age=a;
}
void display()
{
System.out.println("n");
System.out.println("ct");
System.out.println("a");
}
public static void main(String args[])
{
person p1= new person();
p1.set("Dhruv","Dhule",14);
//Change it to set()
p1.display();
}
}
What was the error?
- The error was inside main function.
- You declared set() and in main() block you called setData() here is the Error.
- Actually, there was no function like setData declared. So that's why you got the error.
- This error occurs when you call a function which is not declared by you in the program.
How to fix it?
- Simply change p1.setData() to p1.set() inside main().