Greetings,

I'm having problems with my output. Essentially I am just trying to print out any Cat who is over 3 years old and has Claws. But my output keeps printing out either nothing at all or the 3rd cat 3 times. I've struggled with this for hours trying to figure out why.. any ideas?
This is my driver java file. It will compile and run just fine but it doesn't work like it is supposed to..

import java.util.Scanner;

public class NathanialProg6
{
 public static void main(String[] args)
 {
  Scanner input = new Scanner(System.in);

  Cat cat1 = new Cat();
  Cat cat2 = new Cat();
  Cat cat3 = new Cat();
  int numberOfCats = 3;

  for(int i = 0; i < numberOfCats; 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();

  boolean claws = true;
  System.out.println("Does the cat have claws? T or F: ");
  String answer = input.next();
  if (answer.charAt(0) == 'T' || answer.charAt(0) == 't')
  {
      claws = true;
  }
  else if (answer.charAt(0) == 'F' || answer.charAt(0) == 'f')
  {
      claws = false;
  }
  else { System.out.println("Invalid input must be T or F"); }

  System.out.println("\n");

  cat1.setName(name);
  cat1.setAge(age);
  cat1.setClaws(claws);
  cat2.setName(name);
  cat2.setAge(age);
  cat2.setClaws(claws);
  cat3.setName(name);
  cat3.setAge(age);
  cat3.setClaws(claws);
  }

  System.out.println("The Cats over 3 with claws are: ");

  if(cat1.getAge() > 3 && cat1.getClaws() == true)
  {
      System.out.println(cat1.getName());
      System.out.println(cat1.getAge());
  }
  if(cat2.getAge() > 3 && cat2.getClaws() == true)
  {
      System.out.println(cat2.getName());
      System.out.println(cat2.getAge());
  }
  if(cat3.getAge() > 3 && cat3.getClaws() == true)
  {
      System.out.println(cat3.getName());
      System.out.println(cat3.getAge());
  }   

 }
}

Lines 43-51 you set the same data for all 3 cats, using the data from the third/last pass of you input loop, so when you print them of course you see the same data
You need to set cat1 during the first pass of your loop, cat2 in the second pass (etc).
(ps: An array of Cats would work very well here.)

I dont know much about arrays yet but I did get it figured out. I had create three separate loops for cats 1-3. Painful but it worked.
Thanks

OK, but just for your information, the array would work something like this:
instead of Cat cat1, cat2, cat3 you have

Cat[] cats = new Cat[3];

then the three Cats are cats[0], cats[1] and cats[2]

then the loop is like

 for(int i = 0; i < numberOfCats; i++) {
    cats[i] = new Cat();
    // prompt for name etc
    cats[i].setName(name);
    // etc

just think what will happen when the next exercise is the same but for 10 cats!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.