Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Incompatible Types error

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Incompatible Types error

    Hi all,

    I created a class that has three int variables and one String, and the respective constructor method with all of them.

    public class CalendarioUser {
    int dia;
    int mes;
    int año;
    String nombre;

    however, in the main class (which is in another class file), I can't import the String element because it gives me the error "incompatible types: string cannot be converted to CalendarioUser (the name of my class that contains the variable)"

    This is how I made it:

    public static void main(String[] args) {
    CalendarioUser nombreUser = new CalendarioUser();
    nombreUser = nombreUser.nombre; (this part contains an error)

    I can't figure it out! Why is it giving me this error? isn't supposed to be compatible with Strings if it contains a String variable?
    how should I fix it? Im a beginner with Java code.

    Thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Incompatible Types error

    nombreUser = nombreUser.nombre; (this part contains an error)
    Why is it giving me this error?

    The compiler says:
    "incompatible types: String cannot be converted to CalendarioUser
    nombreUser is defined as a CalendarioUser reference
    nombre is defined as a String.
    There is no way for the compiler to change a String to a CalendarioUser.

    how should I fix it?
    What are you trying to do with that assignment statement?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Incompatible Types error

    Im trying to bring the variable and value of nombre from the other class into this one. That's why I created that piece. The other int elements didn't bring me any errors.

    --- Update ---

    also nombre is an element within the CalendarioUser class. So I dont understand why it doesn't read it as such

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Incompatible Types error

    value of nombre from the other class
    Where is the other class? The posted code does not show two instances of the CalendarioUser class.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Incompatible Types error

    Class "CalendarioUser.java"

    public class CalendarioUser {
       int dia;
       int mes;
       int año;
       String nombre;
     
     
     
    public void calendarioUsuario(String nombreUsuario, int fechaDia, int fechaMes, int fechaAño){
     
    dia = fechaDia;
    mes = fechaMes;
    año = fechaAño;
    nombre = nombreUsuario;
    }

    Class "MainApp.java" (where the Main method is)

    public class MainApp {
     
        public static void main(String[] args) {
          CalendarioUser nombreUser = new CalendarioUser();
          CalendarioUser datosUser = new CalendarioUser();
           nombreUser = nombreUser.nombre;   
     
           datosUser.pedirDatos();
     
           System.out.println(nombreUser + ", tu versículo del día es: 1 Corintios 13.");
           System.out.println("Bendiciones!");
     
     
        }

    Does that help?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Incompatible Types error

    nombreUser = nombreUser.nombre;
    That statement does not make sense. It is not possible to assign the value in a String to a variable that refers to a CalendarioUser object.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Incompatible Types error

    Ok, I see. How can i bring that value into the main class/ method?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Incompatible Types error

    How can i bring that value into the main class/ method?
    What value? Are you asking about the value in the nombre variable in the CalendarioUser class?
      String theName = refToCalendarioUserObject.nombre;   // copy from object to local variable
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Incompatible Types error

    Yeah exactly.

    Ok Ill try that, that "reftoCalendarioUser" is what i want to create.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Incompatible Types error

         CalendarioUser nombreUser = new CalendarioUser();
          CalendarioUser datosUser = new CalendarioUser();
    "refToCalendarioUserObject" is what i want to create.
    In the above code, nombreUser and datosUser are both references To CalendarioUser Objects
    You have 2 already in that code.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Incompatible Types error

    Yes, but one is working and the other is not. One is to refer to a int object inside CalendarioUser and it works,
    the other one is to refer to a String object in the same class, and that's the one that is not working

    I tried this and it didn't work. I think I'm not understanding:

    public static void main(String[] args) {
     
          CalendarioUser datosUser = new CalendarioUser();
     
            String nombreDemo;
            nombreDemo = CalendarioUser.nombre;

    The error now is "A non-static variable can't be referenced from a static context". Is there a solution to this?.

    Thanks!

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Incompatible Types error

    Use the reference to the CalendarioUser class object: datosUser
        nombreDemo = datosUser.nombre;
    do not use the name of the class
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Incompatible Types error

    I just don't understand how to call into a class objects from another class

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Incompatible Types error

    You need a reference to an instance of a class to access its variables and methods.
            CalendarioUser datosUser = new CalendarioUser();    // create an instance and get its address in the variable datosUser
            //  datosUser now references an instance of the CalendarioUser class
            String nombreDemo = datosUser.nombre;   //  use the reference to access a variable in the instance of the class
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] incompatible types
    By muhammad93 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 19th, 2013, 02:20 PM
  2. Incompatible types.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 12th, 2013, 08:36 PM
  3. Incompatible types
    By jadeclan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 25th, 2012, 04:44 PM
  4. [SOLVED] Referencing to Method. Incompatible Types Error.
    By mwebb in forum Object Oriented Programming
    Replies: 12
    Last Post: February 5th, 2012, 03:50 PM
  5. incompatible types
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2011, 10:40 AM

Tags for this Thread