-3

Compiler throws an exception saying:

Cannot find symbol p and x

This program takes user input and prints as per the loop condition. While compiling it throws an error that it cannot find the symbol.

import java.util.Scanner;
class Puppy{
    String name;
    String breed;
    String gender;
    int weight;
    int age;
    int loud;

    void hammer()
    {
        System.out.println("Enter your Dogs name :");
        p[x].name = user_input.next();
        System.out.println("Enter your Dogs Breed :");
        p[x].breed = user_input.next();
        System.out.println("Enter your Dogs Gender :");
        p[x].gender = user_input.next();
        System.out.println("Enter your Dogs weight in Kg:");
        p[x].weight = user_input.nextInt();
        System.out.println("Enter your Dogs age :");
        p[x].age = user_input.nextInt();
        System.out.println("Rate your Dogs Loudness out of 10 :");
        p[x].loud = user_input.nextInt();       
    }

    void jammer()
    {
        System.out.println("Hello my name is" + " " + p[x].name);
        System.out.println("I am a" + " " + p[x].breed);
        System.out.println("I am" + " " + p[x].age + " " + "years old" + " " + p[x].gender);
        System.out.println("I weigh around" + " " + p[x].weight + " " + "kg's");
        System.out.println("I sound this loud" + " " + p[x].loud);  
    }
}

class PuppyTestDrive
{
    public static void main(String []args)
    {
        Scanner user_input = new Scanner(System.in);
        int x = 0;
        Puppy[] p = new Puppy[4];

        while(x < 4)
        {
            p[x] = new Puppy();
            p[x].hammer();
            p[x].jammer();
            x = x + 1;
        }
    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
pintoo
  • 11
  • 1
  • 4
  • 4
    You're trying to use variable 'p' inside function hammer(), but it is a stack variable in the main method(). You need to make it a property of the class. – LordWilmore Jul 06 '16 at 07:39
  • 1
    `hammer()` and `jammer()` methods don't know about p and x – daf Jul 06 '16 at 07:39
  • You are using local variable as global. You need either to declare P and X as global variable or declare them inside hammer() as local. – Radouane ROUFID Jul 06 '16 at 07:41
  • 2
    Hint: don't do this: don't write 100 lines of code to then run the compiler. Just write small sections (of which you think: "this should compile"). Then run the compiler. Then read its error messages **carefully**. Java compiler messages are really good, most often, they tell you exactly what is going on. Then fix the errors, and write another few lines of code. And hint: your names are really bad. The name of a variable should tell you what the variable "is". p and x ... say absolutely nothing. Same for the names of your methods. Don't try to be funny. Nobody loves about badly written code. – GhostCat Jul 06 '16 at 07:43
  • just a detail: You can replace `" " + "some text" + " "` with `" some text "` – dingalapadum Jul 06 '16 at 08:25

1 Answers1

1

Your program should work with the following code:

import java.util.Scanner; 

class Puppy{

    String name;
    String breed;
    String gender;
    int weight;
    int age;
    int loud;

    void hammer()
    {
        Scanner user_input = new Scanner(System.in);
        System.out.println("Enter your Dogs name :");
        this.name = user_input.next();
        System.out.println("Enter your Dogs Breed :");
        this.breed = user_input.next();
        System.out.println("Enter your Dogs Gender :");
        this.gender = user_input.next();
        System.out.println("Enter your Dogs weight in Kg:");
        this.weight = user_input.nextInt();
        System.out.println("Enter your Dogs age :");
        this.age = user_input.nextInt();
        System.out.println("Rate your Dogs Loudness out of 10 :");
        this.loud = user_input.nextInt();       
    }

    void jammer()
    {
        System.out.println("Hello my name is" + " " + this.name);
        System.out.println("I am a" + " " + this.breed);
        System.out.println("I am" + " " + this.age + " " + "years old" + " " + this.gender);
        System.out.println("I weigh around" + " " + this.weight + " " + "kg's");
        System.out.println("I sound this loud" + " " + this.loud);  
    }
}

class PuppyTestDrive {

    public static void main(String []args)
    {                
        int x = 0;
        Puppy[] p = new Puppy[4];

        while(x < 4)
        {
            p[x] = new Puppy();
            p[x].hammer();
            p[x].jammer();
            x = x + 1;
        }
    }
}
wake-0
  • 3,918
  • 5
  • 28
  • 45
CloudPotato
  • 1,255
  • 1
  • 17
  • 32