Results 1 to 15 of 15
  1. #1
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Not compiling :(

    Currently I am learning Java from various tutorials, and in one I had
    Java Code:
    class Puppy{
       
       int puppyAge;
    
       public Puppy(String name){
          // This constructor has one parameter, name.
          System.out.println("Passed Name is :" + name ); 
       }
       public setAge( int age ){
           puppyAge = age;
       }
    
       public getAge( ){
           System.out.println("Puppy's age is :" + puppyAge ); 
           return puppyAge;
       }
       public static void main(String []args){
          /* Object creation */
          Puppy myPuppy = new Puppy( "tommy" );
    
          /* Call class method to set puppy's age */
          myPuppy.setAge( 2 );
    
          /* Call another class method to get puppy's age */
          myPuppy.getAge( );
    
          /* You can access instance variable as follows as well */
          System.out.println("Variable Value :" + myPuppy.puppyAge ); 
       }
    }
    , but for some reason it will not compile, it is probably incomplete in some ways, but I have no idea because I'm new.
    m(_ _)m

    Thanks in advance for any help.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Not compiling :(

    for some reason it will not compile
    What is the compiler message?

    Generally the fact that code does not compile should be regarded as a good thing, since, as written, the code does not express what you mean. Responding to the compiler messages is the way to get the code to do what you want. Of course that implies that you have to understand the messages... And that's where forums like this can be valuable - by explaining what the (often cryptic) messages mean.

    If you compile the code at the command line the messages will be presented in a pretty standard wording that can be copied and posted here.

  3. #3
    Darwanism is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Not compiling :(

    you've done some pretty useless and there is few declarations which are missing here i'll try to modify it for you
    Java Code:
    public class puppy {
    	int puppyage;
    	String puppyname;
    	public puppy(int puppyage, String puppyname){
    		this.puppyage=puppyage;
    		this.puppyname=puppyname;
    		
    	}
    	public puppy(){
    		
    	}
    	/*normally setting value with the constructor is good enough 
    	 * but if you want you can add a setpuppyame and setpuppyage
    	 * to modify them, or edit them if u have a no-org consturctor
    	 */
    	public void setpuppyname(String Name){
    		puppyname=Name;
    	}
    	public void setpuppyage(int age){
    		puppyage=age;
    	}
    	public String getName(){
    		return this.puppyname;
    	}
    	public int getAge(){
    		return this.getAge();
    	}
    	public void showPuppyInfo(){
    		System.out.println("puppy's name = "+getName()+" puppy's age "+getAge());
    	}
    	
    
    	public static void main(String[] args) {
    		/* @doll1 i'm gonna use the no-arg constructor + the setpuppyname 
    		 * setpuppyage methods
    		 */
    		puppy doll1 = new puppy();
    		doll1.setpuppyage(5);
    		doll1.setpuppyname("blabla");
    		doll1.showPuppyInfo();
    		//@doll2 no need for the set methods the constructor will handle
    		//it
    		puppy doll2 = new puppy(15, "barbie");
    		doll2.showPuppyInfo();
    
    	}
    
    }

  4. #4
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Not compiling :(

    Well, it says

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor Puppy(int, String) is undefined

    at Puppy.main(Puppy.java:43)

    But besides that, being new I would appreciate if you could explain what you did to me, from what I see

    Java Code:
     public void setpuppyage(int age){
            puppyage=age;
    seems to be like a function you can call. So for the most part I understand that and below, but what about

    Java Code:
    public class Puppy {
        int puppyage;
        String puppyname;
        public puppy(int puppyage, String puppyname){
            this.puppyage=puppyage;
            this.puppyname=puppyname;
    I understand most of it, it's mainly the "this" thing, because I've never really encountered that before.

    Also, just so you know, "String" isn't being highlighted as syntax in Eclipse for some reason, maybe a glitch?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Not compiling :(

    You might consider dealing with the original compiler messages (whatever they were) and getting that code to compile and run before you change it. There was nothing fundamentally wrong with that code.

  6. #6
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Not compiling :(

    Well, I tried to simplify it

    Java Code:
    class Puppy{
    	
    	int puppyAge;
    	String puppyName;
    	
       public Puppy(){
    	  
       }
       public int setAge(int age){
           puppyAge = age;
       }
       
       public String setName(String name){
    	   puppyName = name;
       }
     
       public showInfo(){
           System.out.println("Puppy's age is : " + puppyAge); 
           System.out.println("Puppy's Name is : " + puppyName);
           return puppyAge;
           return puppyName;
       }
       public static void main(String []args){
    
          Puppy myPuppy = new Puppy();
     
          myPuppy.setAge(2);
          
          myPuppy.setName("Bouncer");
     
          myPuppy.showInfo();
     
          System.out.println("Variable Value :" + myPuppy.puppyAge); 
       }
    }
    and I only get one error rather than like 3

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method showInfo() is undefined for the type Puppy

    at Puppy.main(Puppy.java:31)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Not compiling :(

    The method showInfo() is undefined for the type Puppy
    This is not a compiler message, but rather a message from the IDE's runtime when you try and run the program which tries (badly) to report to you what the compiler would have said. Remember that Java programs are (1) compiled and (2) run. These are separate steps and you should *seriously* consider compiling and running the program from the command line (console/terminal) where these two actions are not conflated and confused.

    Sorry for the rant (but, honestly, NB has a lot to answer for)... In this case the problem is that the Puppy class does not have a showInfo() method. Remember that methods always have a return type (void/int/etc). Without a return type there will be no method, and when you run the program (which you can't really do because it hasn't compiled...) you will get the "showInfo() is undefined" message.

    So, add a return type to showInfo().

    ---

    By the way this was the problem in your original code (in two places).

  8. #8
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Not compiling :(

    But showInfo uses Strings and ints, so which one do I choose for the return type?

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Not compiling :(

    I'm a fan of javadoc comments, so I would add a comment to say what that method does:

    Java Code:
        /**
         * Prints information about the puppy to System.out.
         */
    public ??? showInfo(){
           System.out.println("Puppy's age is : " + puppyAge); 
           System.out.println("Puppy's Name is : " + puppyName);
           return puppyAge;
           return puppyName;
    }
    As the comment suggests (by what it doesn't say) the method is not intended to return anything. So (1) use void as the return type and (2) remove the two return statements.

    ---

    Notice that the return type has nothing to do what types are used within the method. It's all about what type of thing (if anything) is returned to the caller. All methods have return types, constructors do not.

    My preference for adding javadoc comments reflects something deeper: I already know what I want a method to do before I start to write it. I sympathise with the fact that you are playing about and trying to get a feel for Java's syntax, but it's never too early to adopt the rule that the programmer is in charge. Just hacking about until you get something to compile and run isn't good enough. The program (and each method) should do precisely what you intended it to do.

  10. #10
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Not compiling :(

    Java Code:
    public void showInfo(){
           System.out.println("Puppy's age is : " + puppyAge); 
           System.out.println("Puppy's Name is : " + puppyName);
       }
    __________________________________________________ ______________

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    This method must return a result of type int

    at Puppy.setAge(Puppy.java:9)
    at Puppy.main(Puppy.java:25)
    __________________________________________________ ______________

    I did what you said but it's still not working.
    m(_ _)m
    Last edited by Darkzombies; 11-19-2012 at 12:17 AM.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Not compiling :(

    Same deal as before: Decide what you intend the method to do. This will determine what (if anything) the method should be declared to return. Once you have that sorted out make sure the method is declared with the correct return type. If it *does* return something there should be a "return" statement, if it is void then there should not be.

    So, first thing: what do you intend this method to do? In case it's not clear, the problem is with the setAge() method.
    Last edited by pbrockway2; 11-19-2012 at 12:22 AM.

  12. #12
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Not compiling :(

    All the method is supposed to do is print the name and age to output, which it doesn't want to do, nothing needs to be returned, because the method is everythings final destination.

  13. #13
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Not compiling :(

    Ok, I got rid of the showInfo method all together and put it in main (Should've done that in the first place) and of course, I still get an error, but this time it has something to do with the variable.

    __________________________________________________ _____________________

    Java Code:
    class Puppy{
         
        int puppyAge;
         
       public Puppy(String name){
           
       }
       public int setAge(int age){
           puppyAge = age;
       }
      
       public static void main(String []args){
     
          Puppy myPuppy = new Puppy("Bouncer");
      
          myPuppy.setAge(2);
      
          System.out.println("Puppy's age is : " + puppyAge); 
          System.out.println("Puppy's Name is : " + name);
       }
    }
    Error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot make a static reference to the non-static field puppyAge
    name cannot be resolved to a variable

    at Puppy.main(Puppy.java:18)


    __________________________________________________ ____________________

    I assume I'm supposed to convert the integer to a string? I don't know the command for that, if there even is one.

    __________________________________________________ ____________________

    EDIT: Fixed it!!

    Java Code:
    class Puppy{
         
       static int puppyAge;
       static String puppyName;
         
       public Puppy(){
           
       }
       public int setAge(int age){
           return puppyAge = age;
       }
       public String setName(String name){
    	   return puppyName = name;
       }
      
       public static void main(String []args){
     
          Puppy myPuppy = new Puppy();
          
          myPuppy.setName("Bouncer");
      
          myPuppy.setAge(2);
          
          
          
          System.out.println("Puppy's age is : " + puppyAge); 
    	System.out.println("Puppy's Name is : " + puppyName);
       }
    }
    Last edited by Darkzombies; 11-19-2012 at 03:22 AM.

  14. #14
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Re: Not compiling :(

    I would sit down and think about it a little more.
    static may have worked in this case, but is it really what you wanted to do?

    Look up static and see what it means over the lifecycle of an application, then think about what affect this has on the application and if its how you really wanted it to work.

    What you have done with the lines

    System.out.println("Puppy's age is : " + puppyAge);
    System.out.println("Puppy's Name is : " + puppyName);

    Is called the variables statically. It would probably be safer to create methods which return the puppyAge and puppyName and use these methods to return the information you are after.
    Last edited by sibernewf; 11-19-2012 at 06:36 AM.

  15. #15
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Not compiling :(

    I tried that in past attempts, but creating a method to display the text gives more problems then its worth, at least until I can figure it out that is.

    __________________________________________________ _______________________________________________

    EDIT: Well, I figured it out, it was bound to happen sometime.

    Java Code:
    class Employee{
    	public String name;
    	private int salary;
    	
    	public Employee(String empName){
    		name = empName;
    	}
    	public void setSalary(int empSalary){
    		salary = empSalary;
    	}
    	public void printEmp(){
    		System.out.println("Name: " + name);
    		System.out.println("Salary: " + salary);
    	}
    	public static void main(String []args){
    		Employee empOne = new Employee("Bob");
    		empOne.setSalary(650);
    		empOne.printEmp();
    	}
    		
    }
    Also kind of helpful because I can do this

    Java Code:
    class Employee{
    	public String name;
    	private int salary;
    	
    	public Employee(String empName){
    		name = empName;
    	}
    	public void setSalary(int empSalary){
    		salary = empSalary;
    	}
    	public void printEmp(){
    		System.out.println("____________________");
    		System.out.println("Name: " + name);
    		System.out.println("Salary: " + salary);
    		System.out.println("____________________");
    	}
    	public static void main(String []args){
    		Employee empOne = new Employee("Bob");
    		Employee empTwo = new Employee("Zak");
    		empTwo.setSalary(1250);
    		empTwo.printEmp();
    		empOne.setSalary(650);
    		empOne.printEmp();
    	}
    		
    }
    Although the variables are overwritten, so they will only remember the last one, but thats what arrays are for (Once I learn how to use them)
    Last edited by Darkzombies; 11-19-2012 at 07:49 AM.

Similar Threads

  1. Compiling
    By yavo in forum New To Java
    Replies: 1
    Last Post: 12-24-2011, 02:23 PM
  2. Compiling help!
    By cloverrea in forum New To Java
    Replies: 9
    Last Post: 10-12-2011, 06:05 AM
  3. Compiling a Servlet!!
    By f22raptor in forum New To Java
    Replies: 0
    Last Post: 10-04-2011, 06:12 PM
  4. About compiling
    By edh in forum Advanced Java
    Replies: 2
    Last Post: 06-16-2011, 10:47 AM
  5. how to get compiling
    By philgrek in forum New To Java
    Replies: 1
    Last Post: 05-26-2010, 12:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •