Results 1 to 6 of 6
  1. #1
    mustachio is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Cannot find symbol class

    I wrote a new class called Pet and I'm trying to get it connected to a pre-written class called Assignment4 for, well, an assignment. However, I can't link the two together. They're saved in the same folder on my computer, so I don't understand the problem. I get the error "cannot find symbol constructor Pet()". Here is the complete code. Thanks in advance for any help.

    First Program
    Java Code:
    public class Pet
    {
    	private static String petName;
    	private static String type;
    	private static BirthInfo birth;
    
    	public Pet(String petName, String type)
    	{
    		petName = "?";
    		type = "?";
    	}
    
    	public String getPetName()
    	{
    		return petName;
    	}
    
    	public String getType()
    	{
    		return type;
    	}
    
    	public BirthInfo getBirthInfo()
    	{
    		return birth;
    	}
    
    	public void setPetName (String pName)
    	{
    		petName = pName;
    	}
    
    	public void setType (String pType)
    	{
    		type = pType;
    	}
    
    	public void setBirthInfo(int date, int month, int year, String place)
    	{
    		birth.setDate(date);
    		birth.setMonth(month);
    		birth.setYear(year);
    		birth.setPlace(place);
    	}
    
    	public String toString()
    	{
    		return "\nPet Name:\t\t" + petName + "\nType:\t\t\t" + type +
    			   "\nBirth Information:\t" + birth + "\n\n";
    	}
    }
    Second program
    Java Code:
    import java.io.*;    //to use InputStreamReader and BufferedReader
    import java.util.*;
    
    public class Assignment4
     {
      public static void main (String[] args)
       {
           // local variables, can be accessed anywhere from the main method
           char input1 = 'Z';
           String inputInfo;
           String name, type, place;
           int date, month, year;
           String line = new String();
    
           // instantiate a Pet object
           Pet pet1 = new Pet();
    
           printMenu();
    
           //Create a Scanner object to read user input
           Scanner scan = new Scanner(System.in);
    
    
           do  // will ask for user input
            {
             System.out.println("What action would you like to perform?");
             line = scan.nextLine();
    
             if (line.length() == 1)
              {
               input1 = line.charAt(0);
               input1 = Character.toUpperCase(input1);
    
               // matches one of the case statement
               switch (input1)
                {
                 case 'A':   //Add Pet
                   System.out.print("Please enter the pet information:\n");
                   System.out.print("Enter a pet name:\n");
                   name = scan.nextLine();
                   pet1.setPetName(name);
    
                   System.out.print("Enter its type:\n");
                   type = scan.nextLine();
                   pet1.setType(type);
    
                   System.out.print("Enter its birth date:\n");
                   date = Integer.parseInt(scan.nextLine());
                   System.out.print("Enter its birth month:\n");
                   month = Integer.parseInt(scan.nextLine());
                   System.out.print("Enter its birth year:\n");
                   year = Integer.parseInt(scan.nextLine());
                   System.out.print("Enter its birth place:\n");
                   place = scan.nextLine();
                   pet1.setBirthInfo(date, month, year, place);
    
                   break;
                 case 'D':   //Display Pet
                   System.out.print(pet1);
                   break;
                 case 'Q':   //Quit
                   break;
                 case '?':   //Display Menu
                   printMenu();
                   break;
                 default:
                   System.out.print("Unknown action\n");
                   break;
                }
              }
             else
              {
               System.out.print("Unknown action\n");
              }
            } while (input1 != 'Q' || line.length() != 1);
       }
    
      /** The method printMenu displays the menu to a user **/
      public static void printMenu()
       {
         System.out.print("Choice\t\tAction\n" +
                            "------\t\t------\n" +
                            "A\t\tAdd Pet\n" +
                            "D\t\tDisplay Pet\n" +
                            "Q\t\tQuit\n" +
                            "?\t\tDisplay Help\n\n");
      }
    }
    PS the BirthInfo references are for another class that I wrote, but there don't seem to be any problems linking it with the other programs.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Read teh error message very carefully. How many parameters does your Pet constructor expect? How many parameters are you passing it?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,712
    Rep Power
    14

    Default

    "cannot find symbol constructor Pet()"
    That is saying that you don't have a Pet constructor that takes no arguments. As written the constructor you do have requires name and type arguments. (although I don't know why. what is that constructor supposed to do exactly?)

  4. #4
    mustachio is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Ah yes, not sure why I added those arguments in there. I took them out and everything seems to be working fine. Thank you!

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    In future please try to read and comprehend what the error message says. This one was relatively easy to understand.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,712
    Rep Power
    14

    Default

    Another thing you might want to think about: what is petName in the Pet class supposed to represent? Something static (ie common to pets as a whole)?

Similar Threads

  1. Cannot find symbol class, but why not?
    By gnng in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 03:57 PM
  2. Extended Class - Cannot Find Symbol
    By javak in forum New To Java
    Replies: 15
    Last Post: 02-27-2011, 05:43 PM
  3. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  4. Replies: 3
    Last Post: 05-01-2008, 08:30 AM
  5. cannot find symbol class error
    By po0oker in forum New To Java
    Replies: 5
    Last Post: 10-31-2007, 03:52 PM

Posting Permissions

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