Part_Time_Nerd
Part_Time_Nerd

Reputation: 1014

Why is my Java double variable type coming out to 0.0 when I put in an integer?

I am using a driver class to create an object of another class. When I enter the pet weight or integer the number comes out to 0.0. All of the weight variables are declared as double so I do not know why it is doing this.

  import java.util.Scanner;

    public class PetAssignment {

        public static void main(String[] args) 
        {
            String nameAndType;
            int yrs;
            double lbs;

            //Scanner object for the keyboard input
            Scanner answers = new Scanner(System.in);

            //Pet objects used for calling accessor methods
            Pet petName = new Pet();
            Pet petType = new Pet();
            Pet petAge = new Pet();
            Pet petWeight = new Pet();

            //A bunch of other code and pet attributes

            //Input for the weight of pet
            System.out.print("How many pounds does your pet weight? ");
            lbs = answers.nextDouble();
            petName.setWeight(lbs);

            //Print out of the user's answers
            System.out.println(""); 
            System.out.println("You have a "+ petType.getType() + ". That is named " 
                + petName.getName()+ " and is " 
                + petAge.getAge() + " years old and weighs " 
                + petWeight.getWeight() + " lbs.");         
        }
    }

Here is my Pet Class

 public class Pet 
    {
      private String name;
      private String type;
      private int age;
      private double weight;    
      /*
       * a bunch of other code
       */
      public void setWeight(double petWeight)
      {
          weight = petWeight;
      }
      /*
       * a bunch of other code
       */
      public double getWeight()
      {
          return weight;
      }
    }

Upvotes: 0

Views: 1979

Answers (7)

Part_Time_Nerd
Part_Time_Nerd

Reputation: 1014

Thanks everyone for the help. I feel like an idiot for having such a simple mistake. I also simplified my code with only one instance of pet class.

import java.util.Scanner;

    public class PetAssignment {

        public static void main(String[] args) 
        {
            String nameAndType;
            int yrs;
            double lbs;

            //Scanner object for the keyboard input
            Scanner answers = new Scanner(System.in);

            //Pet objects used for calling accessor methods
            Pet pet = new Pet();

            //A bunch of other code and pet attributes

            //Input for the weight of pet
            System.out.print("How many pounds does your pet weight? ");
            lbs = answers.nextDouble();
            pet.setWeight(lbs);

            //Print out of the user's answers
            System.out.println(""); 
            System.out.println("You have a "+ pet.getType() + ". That is named " 
                + pet.getName()+ " and is " 
                + pet.getAge() + " years old and weighs " 
                + pet.getWeight() + " lbs.");         
        }
    }

Upvotes: 1

Yubaraj
Yubaraj

Reputation: 3988

You are doing wrong in setting and getting the weight of yours Pet. You need to use the same object to set and get value. You are setting your Pet weight in petName object and getting from petWeight object. That's why you are getting 0.0 which is the default value of double. To solve your problem use same object to set and get your Pet's weight.

Example:

petWeight.setWeight(lbs); //setting the value in petWight object
petWeight.getWeight(); // it will returns the same value that you set.

Upvotes: 0

ahmed khattab
ahmed khattab

Reputation: 2501

the wrong is that you use this code for setting value

 petName.setWeight(lbs);

and use this for retrieving value

 Pet petWeight = new Pet();

they are 2 different objects you must use the same object for 2 statements in setting , retrieving by putting

petWeight.setWeight(lbs);

instead of

petName.setWeight(lbs);

will solve it

Upvotes: 2

Gonza
Gonza

Reputation: 303

The problem is at line 34, check what are you doing here

petName.setWeight(lbs);

But when you show the output

System.out.println("You have a "+ petType.getType() + ". That is named " 
            + petName.getName()+ " and is " 
            + petAge.getAge() + " years old and weighs " 
            + petWeight.getWeight() + " lbs.");

Do you see it? You are showing the wieight of "petWeight but the scanner is setting the petName object, please debug and checkout that.

System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petWeight.setWeight(lbs); 

And the result is

 You have a null. That is named null and is 0 years old and weighs 1.5 lbs. 

Of courst the other attributes are null.

I hope this useful. Regards!

Upvotes: 1

Santiago Alzate
Santiago Alzate

Reputation: 407

The problem I'm seeing is you are creating multiple Pet objects and assign the pet weight to the "petName" object but then you are trying to add to the output the Weight of the "petWeight" object. Try with the following:

        //Input for the weight of pet
        System.out.print("How many pounds does your pet weight? ");
        lbs = answers.nextDouble();
        petWeight.setWeight(lbs);

        //Print out of the user's answers
        System.out.println(""); 
        System.out.println("You have a "+ petType.getType() + ". That is named " 
            + petName.getName()+ " and is " 
            + petAge.getAge() + " years old and weighs " 
            + petWeight.getWeight() + " lbs.");         
    }
}

Also, I would recommend to use only one object "pet" and assign every value to that one, as well as using only that one on the system.out

Upvotes: 1

Bjørn Vårdal
Bjørn Vårdal

Reputation: 174

If the age or weight doesn't have the expected formatting, I would recommend checking the return type of getAge() and getWeight. If it doesn't match the field, it will be converted when returned, at least in the int->double case (I believe double->int would require a cast).

But as already mentioned, adding .0 is the expected behaviour for double. If you don't want it, you can explicitly cast to int.

Upvotes: 1

ejmejm
ejmejm

Reputation: 86

The first important thing to this problem is that you only need one instance of the class "Pet". One pet can hold all the variables you need. For example:

Pet pet = new Pet();
System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petName.setWeight(lbs);

 System.out.println(""); 
        System.out.println("You have a "+ pet.getType() + ". That   is named " 
            + pet.getName()+ " and is " 
            + pet.getAge() + " years old and weighs " 
            + pet.getWeight() + " lbs.");

The way you have written it essentially creates 4 different containers when you only need one. You assigned the weight to the petWeight container but then tried to get the weight from the petName container, which resulted in you retrieving the wrong variable. With only one container, or instance, called "pet" in this case, this issue shouldn't occur.

Upvotes: 2

Related Questions