Friends,
I'm taking a Java programming class online and it seems that the lecture nor the book explains in detail the finer points needed for my programs to work. Thus far, I've gotten by with what I know of prior languages and trial and error. However, I'm having problems with this driver class program and I need it finished by Friday and I can't get it to compile so I can see if the results it is giving me is correct.

The problem centers around the "if statement" if (cat (this.age >= 3) && (this.declawed == false)and the print.out "get" lines. The errors are nonstatic variable and can't find symbol. Can anyone point me in the right direction. (I have the cat.java program and it compiled nicely.) Thanks!!

import java.util.*;
public class OwensDianaWeek6Prog
{
    public static void main (String[] args)
    {
       cat myCat1 = new cat();  //intializes cat 1
	   cat myCat2 = new cat();  //intializes cat 2
	   cat myCat3 = new cat();  //intializes cat 3


       Scanner input = new Scanner(System.in);
       for (int i=0; i<3; i++)
       {
          System.out.println("Enter the name of cat" + (i+1) + ":");
          String name = input.next();
          System.out.println("Enter the age of cat" + (i+1) + ":");
          int age = input.nextInt();
          System.out.println("Enter the weight of cat" + (i+1) + ":");
          double weight = input.nextDouble();
          System.out.println("Enter the breed of cat" + (i+1) + ":");
          String breed = input.next();
          System.out.println("Is cat" + (i+1) +  "declawed? True or False:");
          boolean declawed = input.nextBoolean();
          myCat1.set(name, age, weight);
	      myCat1.setBreed(breed);
          myCat2.set(name, age, weight);
	      myCat2.setBreed(breed);
	      myCat3.set(name, age, weight);
          myCat3.setBreed(breed);
       } //end for


       if (cat (this.age >= 3) && (this.declawed == false))
       {
		     System.out.println("The cats over 3 with claws are:");
		     System.out.println("Name: " + getName(name));
		     System.out.println("Age: " + getAge(age) + "Years Old");
       } //end if

    }// main
} // end class OwensDianaWeek6Prog

`this` refers to the current object in consideration. Static methods don't have an instance associated with them and hence `this` is not allowed in static methods.

`this` refers to the current object in consideration. Static methods don't have an instance associated with them and hence `this` is not allowed in static methods.

I took out the instance variables and managed to get the program to compile. The program is working somewhat. However, it's suppose to give me the answer "The cats over 3 with claws are:..." after information for all three cats have been inputted. As written now, it lets me put in info for the first two cats. It then reports back the name and age of cat 2 because it fits the "if" statement criteria before going on to ask about cat 3. How do I get the program to wait until information on all cats have been inputted before returning the answer that fits the "if" statement criteria?

Here is my current code:

import java.util.*;
public class Week6Prog
{
    public static void main (String[] args)
    {
       cat myCat1 = new cat();  //intializes cat 1
	   cat myCat2 = new cat();  //intializes cat 2
	   cat myCat3 = new cat();  //intializes cat 3

       Scanner input = new Scanner(System.in);
       for (int i=0; i<3; i++)
       {
          System.out.print("Enter the name of cat " + (i+1) + ": ");
          String name = input.next();
          System.out.print("Enter the age of cat " + (i+1) + ": ");
          int age = input.nextInt();
          System.out.print("Enter the weight of cat " + (i+1) + ": ");
          double weight = input.nextDouble();
          System.out.print("Enter the breed of cat " + (i+1) + ": ");
          String breed = input.next();
          System.out.print("Does cat " + (i+1) +  " have claws? true or false: ");
          boolean declawed = input.nextBoolean();
          System.out.println("");
          myCat1.set(name, age, weight); //set myCat1 variables for name, age and weight
	 myCat1.setBreed(breed);        //set myCat1 variable for breed
          myCat2.set(name, age, weight); //set myCat2 variables for name, age and weight
	 myCat2.setBreed(breed);        //set myCat1 variable for breed
	 myCat3.set(name, age, weight); //set myCat3 variables for name, age and weight
          myCat3.setBreed(breed);        //set myCat1 variable for breed

        if ((declawed) && (age>=3))
	 {
	   	System.out.println("The cats over 3 with claws are:");
	   	System.out.println("Name: " + name);
	   	System.out.println("Age: " + age + " Years Old");
          } //end if


       } //end for


    }// main
} // end class Week6Prog

Edited 6 Years Ago by darling332001: n/a

How do I get the program to wait until information on all cats have been inputted before returning the answer that fits the "if"

Are you gathering the info inside a loop?
Where is the end of the loop?
Move the question/"if" outside of the loop so that it is not tested for until ALL of the info has be inputted.

Are you gathering the info inside a loop?
Where is the end of the loop?
Move the question/"if" outside of the loop so that it is not tested for until ALL of the info has be inputted.

When I move it outside the loop I get the errors:
cannot find symbol symbol : variable name
cannot find symbol symbol : variable age

-- Diana

Define the variables outside the loop so they have a large enough scope to be seen where they need to be seen.

import java.util.*;

class cat{
	String name;
	int age;
	double weight;
	String breed;
	boolean declawed;
	public cat(String s, int n, double d, String bs, boolean dec){
		age=n;
		name = s;
		weight = d;
		breed = bs;
		declawed = dec;
	}
public	String getName(){
	return name;
} 	
public int getAge(){
	return age;
}
}
public class OwensDianaWeek6Prog
{
    public static void main (String[] args)
    {
       cat MyCat[] = new cat[3];


       Scanner input = new Scanner(System.in);
       for (int i=0; i<3; i++)
       {
          System.out.println("Enter the name of cat" + (i+1) + ":");
          String name = input.next();
          System.out.println("Enter the age of cat" + (i+1) + ":");
          int age = input.nextInt();
          System.out.println("Enter the weight of cat" + (i+1) + ":");
          double weight = input.nextDouble();
          System.out.println("Enter the breed of cat" + (i+1) + ":");
          String breed = input.next();
          System.out.println("Is cat" + (i+1) +  "declawed? True or False:");
          boolean declawed = input.nextBoolean();
          MyCat[i] = new cat(name, age, weight,breed,declawed);
          }
          
		  System.out.println("The cats over 3 with claws are:");	
		for (int i=0; i<3; i++)
       if ((MyCat[i].age >= 3) && (MyCat[i].declawed == false)){
		     System.out.println("Name: " + MyCat[i].getName());
		     System.out.println("Age: " + MyCat[i].getAge() + "Years Old");
       } //end if

    } // main
} // end class OwensDianaWeek6Prog

Edited 6 Years Ago by ~s.o.s~: Added code tags; please learn to use them.

This article has been dead for over six months. Start a new discussion instead.