1

I am supposed to make the program give the number of pets and the percentage of how many that are below 5lbs, 5-10lbs, and over 10lbs. The program keeps repeating the statements and I don't know why. I've been working on this problem for the past couple of days and I still can't figure it out. At times it seems like I fixed it but then later on it happens again. Can anyone clarify why this is happening? I need help please.

import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.lang.Object;

public class SetPetWeight 
{
  public static void main(String[] args) {
    ArrayList<Pet> list = new ArrayList<Pet>();
    String name;
    String answer;
    int age;
    Double weight;
    Scanner keyboard = new Scanner(System.in);

    do
{

      System.out.println("Enter a String for Pet name: ");
      name = keyboard.next(); 

      System.out.println("Enter an int for Pet age: ");
      age = keyboard.nextInt();
      if(age <= 0)
        System.out.println("Error! Enter a real age");

      System.out.println("Enter a double for Pet weight: ");
      weight = keyboard.nextDouble();
      if(weight <= 0)
        System.out.println("Error! Enter a real weight");

                do
      {
        System.out.println("Do you want to enter another pet? Y/N");
        answer = keyboard.nextLine();
          keyboard.nextLine();
      } while (answer.equalsIgnoreCase("Y"));


    } while (name.length() < 0 && age < 0 && weight < 0);



    System.out.println("The weight is now sorted by weight!");
    Collections.sort(list, Pet.SortByWeight);
    for (Pet p2 : list)
      p2.writeOutput();

    int average1 = 0;
    int average2 = 0;
    int average3 = 0;


    for (Pet p : list)
    {
      if(p.getWeight() >= 0 && p.getWeight() <= 5)
      {
        ++average1;
      }
      else if(p.getWeight() >= 5 && p.getWeight() <= 10)
      {
        ++average2;
      }
      else if(p.getWeight() > 10)
      {
        ++average3;
      }
      System.out.println("The average of pets under 5 pounds:" + average1);
      System.out.println("The average of pets between 5 and 10 pounds:" +  average2);
      System.out.println("The average of pets over 10 pounds:" + average3);


    }
  }
}

Pet Class that is used for the SetPetWeight class and is compiled correctly and is used for the array.

 import java.util.*;

    public class Pet {
        private String name;
        private Integer age; // in years
        private double weight; // in pounds

        public void writeOutput() {
            System.out.println("Name: " + name);
            System.out.println("Age: " + age + " years");
            System.out.println("Weight: " + weight + " pounds");
        }

        public void set(String newName) {
            name = newName;
            // age and weight are unchanged.
        }

        public void set(int newAge) {
            if (newAge <= 0) {
                System.out.println("Error: illegal age.");
                System.exit(0);
            } else
                age = newAge;
            // name and weight are unchanged.
        }

        public void set(double newWeight) {
            if (newWeight <= 0) {
                System.out.println("Error: illegal weight.");
                System.exit(0);
            } else
                weight = newWeight;
            // name and age are unchanged.
        }

        public Pet(String name, int age, double weight) {
            this.name = name;
            this.age = age;
            this.weight = weight;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public double getWeight() {
            return weight;
        }

        public static Comparator<Pet> SortByWeight = new Comparator<Pet>() 
        {
          public int compare(Pet pet1, Pet pet2)
          {
            return (int)(pet1.getWeight() - pet2.getWeight());
          }
        };
    }

2 Answers 2

1

Move keyboard.nextLine(); after weight = keyboard.nextDouble(); to consume the dangling newline character .

Sign up to request clarification or add additional context in comments.

1 Comment

The Y/N is excluding everything but when I put Y it repeats the question of "Do you want to enter..."
0

There are a few changes you may want to make. 1. Every time you retrieve user input for pet's name, age and weight. You may want to create an object of type pet to store this values and then add this to your ArrayList. Before you ask them if they wan to add another pet. 2. Remove the outer do while loop, and change the inter loop to include the "Enter a String for pet name: ". 3. Also check for validation for input.

 do {
  System.out.println("Enter a String for Pet name: ");
  name = keyboard.next(); 
  System.out.println("Enter an int for Pet age: ");
  age = keyboard.nextInt();
  if(age <= 0)
    System.out.println("Error! Enter a real age");

  System.out.println("Enter a double for Pet weight: ");
  weight = keyboard.nextDouble();
  keyboard.nextLine();
  if(weight <= 0)
      System.out.println("Error! Enter a real weight");

  System.out.println("Do you want to enter another pet? Y/N");
  answer = keyboard.nextLine();
  keyboard.nextLine();
  } while (answer.equalsIgnoreCase("Y"));

2 Comments

how do I include Enter a string for pet name
The reason why it repeat was that you had it in a loop. It is important you check your indentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.