3

I know it's a silly silly program. It's just to practice constructors.

public class PetRecord {
private String name = "Bob";
private int age;
private double weight;

public PetRecord(String initialName, int initialAge, double initialWeight) {
    name = initialName;

    if(initialAge < 0 || weight < 0) {
        System.out.println("Error: Age or weight cannot be negative");
        System.exit(0);
    }
        else {
            age = initialAge;
            weight = initialWeight;

        }
    }


public PetRecord(String initialName) {
    name = initialName;
}
public void output() {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Weight: " + weight);
}
public void setAll(String newName, int newAge, double newWeight) {
    name = newName;
    if ((newAge < 0) || (newWeight < 0)) {
    System.out.println("Error: Negative age or weight.");
    System.exit(0);
    }
    else { 
    age = newAge;
    weight = newWeight;
        }
    }
public void review() {
    if(age < 8 && weight < 8) {
        System.out.println("Your pets weight and age are healthy.");
    }
    if(age >= 8 && weight >=8) {
        System.out.println("Your pets weight and age are unhealthy.");
    }
    else {
        System.out.println("Come to my office for a proper assessment.");
    }
 }

}

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    PetRecord d = new PetRecord("Bob");
    System.out.println("Please check if our current records are correct.");
    d.output();

    System.out.println("Are they correct?");
    String answer = input.nextLine();
    String yes = "Yes";
    String no = "No";
    if((answer.equals(yes))) {
        System.exit(0);
    }
    if(answer.equals(no)) {
        System.out.println("Please enter your pets name.");
        String correctName = input.nextLine();
        System.out.println("Age?");
        int correctAge = input.nextInt();
        System.out.println("Weight?");
        double correctWeight = input.nextDouble();

        d.setAll(correctName, correctAge, correctWeight);
        System.out.println("Your updated records say: ");
        d.output();

        System.out.println("Would you like a health review?");
        String answer1 = input.nextLine();

        String yes1 = "yes";
        String no1 = "no";

        if(answer1.equals(yes1)) {
            d.review();
        }
        if(answer1.equals(no1)) {
            System.out.println("Thank you for using PetSystem. Goodbye.");
            System.exit(0);
        }




    }
  }
}

My program accepts input for String answer, but my program will not accept String answer1. I can't even type anything in the console after the program asks you if you would like a health review.

Stephen-Wisniewski
  • 291
  • 1
  • 2
  • 12

2 Answers2

1

The issue comes from here

 System.out.println("Your updated records say: ");
    d.output();

Because you have printed something out in the middle of accepting input, you most likely have an extra newline token you have not dealt with. Prior to asking the "health review" question place the following code.

while (input.hasNextLine()) {
    input.nextLine();
}

This will make sure that you clear out any extra tokens before continuing to accept user input.

bhooks
  • 410
  • 5
  • 16
0

you can,t type anything in the console after "if you would like a health review." because you code only runs ones you should put it in a loop to make it run more than once