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 7 of 7

Thread: Main.java expects Person.java behavior to work. How can I solve this?

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Main.java expects Person.java behavior to work. How can I solve this?

    Good afternoon, everybody. The Main class expects the Person class to have a specific behavior, so it will only work if I implement that behavior. What should I put in the Person.java file to make Main.java to run?

    Main.java code:
    package br.xxx.unitOne.exOne;
     
    import br.xxx.unitOne.exOne.lib.Person;
     
    public class Main{
        public static void main(String args[]){
            Person myName = new Person("My Name");
     
            System.out.println(myName.helloWorld());
     
        }
    }

    I would appreciate so much if someone could help me. Thanks.
    Last edited by joelpub; September 30th, 2021 at 09:11 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,384
    Thanks
    54
    Thanked 2,637 Times in 2,587 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    What error messages are you getting with this code?
    If there are no compiler errors, what happens when you execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Quote Originally Posted by Norm View Post
    What error messages are you getting with this code?
    If there are no compiler errors, what happens when you execute the code?
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Person cannot be resolved to a type
    	Person cannot be resolved to a type
     
    	at br.xxx.unitOne.hello_person/br.xxx.unitOne.exOne.Main.main(Main.java:7)

    This is part of a simple exercise but I'm a beginner and don't know how to analyze the code and find out what is missing. I need to write something in the Person.java class (it's empty) and I don't know what it is.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,384
    Thanks
    54
    Thanked 2,637 Times in 2,587 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Person cannot be resolved to a type
    The compiler is looking for a class named Person. The Person class's class file must be on the class path so that the compiler can find it.
    Do you have a Person class? Where is it located? Did you compile it so that there is a Person.class file somewhere?

    The compiler needs to find the Person.class file when the Main.java file is compiled.

    How are you compiling the java file? What command are you using?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Quote Originally Posted by Norm View Post
    The compiler is looking for a class named Person. The Person class's class file must be on the class path so that the compiler can find it.
    Do you have a Person class? Where is it located? Did you compile it so that there is a Person.class file somewhere?

    The compiler needs to find the Person.class file when the Main.java file is compiled.

    How are you compiling the java file? What command are you using?
    I have created a Person.java file in the exact path (br.xxx.unitOne.exOne.lib.Person), the compiler finds it. But I don't know what to put inside Person.java to make Main.java work.

    For exemple, when I write the code below inside Person.java
    package br.xxx.unitOne.exOne.lib;
     
    public class Person {
     
    }

    it returns

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The constructor Person(String) is undefined
    	The method helloWorld() is undefined for the type Person
     
    	at br.xxx.unitOne.hello_person/br.xxx.unitOne.exOne.Main.main(Main.java:7)

    I think I should define the constructor Person(String) and the method helloWorld(). I don't have a clue how to do that. I have been reading about it but it seems impossible for me to do it.

    Note: it is an exercise where I should be able to write the Person.java behavior just by looking to the Main.java code. It will make Main.java work after that. Can you give me some light? (The Main.java code is in the first message of the topic)

    Thank you so much for your patience.
    Last edited by joelpub; September 30th, 2021 at 09:16 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,384
    Thanks
    54
    Thanked 2,637 Times in 2,587 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    I should define the constructor Person(String) and the method helloWorld()
    Yes that sounds right.
    Look at the tutorial on how to create a constructor:
    https://docs.oracle.com/javase/tutor...structors.html
    and how to create methods:
    https://docs.oracle.com/javase/tutor...O/methods.html

    For more information on how to do different things see the items in the tutorial via the links on this page:
    https://docs.oracle.com/javase/tutor...ybigindex.html
    Save that last link for future reference.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main.java expects Person.java behavior to work. How can I solve this?

    Create a person class and add a parameterized constructor to accept the value passing from the main method at the time of creating person object.then run it.

Similar Threads

  1. [SOLVED] Java class Person: with friend_data.txt issue:
    By Mvxexty1001 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: May 7th, 2014, 05:41 AM
  2. Java strange thread behavior
    By Bingo90 in forum Threads
    Replies: 7
    Last Post: October 19th, 2013, 10:53 AM
  3. Replies: 1
    Last Post: September 27th, 2012, 03:55 AM
  4. Is there any intelligent person in the Java programming? plz
    By alaseier in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 4th, 2010, 02:12 PM