Help with java classes!! I cannot make my program work!!!?
Help with java classes!! I cannot make my program work!!!?
Here’s the class I want to make:
import java.util.*;
public class ClaseFecha {
//declaracion de variables
String dia, mes;
public void SetDia(String d)
{
dia=d;
}
public String getDia() {
return dia;
}
public void setMes(String m) {
mes=m;
}
public String getMes() {
return mes;
}
public String toString() {
return dia+”-”+mes;
}
}
Here is the main:
import java.util.*;
public class Fecha {
public static Scanner tecla=new Scanner(System.in);
public static void main(String[] args) {
//Crea el objeto con el metodo constructor.
Fecha f=new Fecha();
String d,m;
m=”0″;
d=”0″;
d.setDia(“06″);
m.setMes(“10″);
System.out.println(“La fecha de hoy es: “+f.toString());
//modificar
System.out.print(“Qué día es hoy (dd)”);
d=tecla.nextLine();
d.setDia(d);
System.out.print(“Qué mes es hoy (mm)”);
m=tecla.nextLine();
m.setMes(m);
System.out.println(“Tu Fecha modificada: “+f.toString());
}
}
I get this errors:
Fecha.java:19: cannot find symbol
symbol : method setDia(java.lang.String)
location: class java.lang.String
d.setDia(“06″);
^
Fecha.java:20: cannot find symbol
symbol : method setMes(java.lang.String)
location: class java.lang.String
m.setMes(“10″);
^
Fecha.java:26: cannot find symbol
symbol : method setDia(java.lang.String)
location: class java.lang.String
d.setDia(d);
^
Fecha.java:29: cannot find symbol
symbol : method setMes(java.lang.String)
location: class java.lang.String
m.setMes(m);
^
4 errors
Best answer:
You are declaring d and m as Strings and start by setting them equal to “0″. That is fine. But then you are trying to use methods as if they were ClaseFecha objects. If you want to use the setDia, getDia, setMes, getMes methods, you need to declare d and m as new ClaseFecha objects. Also, when you do that, you will not be able to use the d = “0″ and m = “0″ statements. But that is what the setDia and setMes methods are for, if I understand your code.
Your errors are because you are declaring variables as one type, but trying to use methods as if they were a different object type.
Tags: Cannot, classes, HELP, java, program, work
Under Forum

Leave a Comment for Help with java classes!! I cannot make my program work!!!?
You must be logged in to post a comment.
Trackback this post | Subscribe to the comments via RSS Feed