Need help with loop


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Need help with loop

  1. #1
    Join Date
    Mar 2007
    Posts
    2

    Need help with loop

    I have a sinple problem that for the life of me cannot solve. My problem is the program that I have compiles and works fine, but I can only enter one set of data instead of three, I have tried many different loops that totally screws up the code.I know it is probable simple but I cannot see it. Can anyone show me what I need to do. Yes I am quite new to java so your help is appreciated.My code is posted below


    /*This follwing statement is the main method of the program.*/
    import java.util.*;

    public class driverprogram
    {
    public static void main(String[] args)
    {
    System.out.println("This program will allow you to enter information about three cats.");
    System.out.println("The user will see all the informtion entered and then the system ");
    System.out.println("will also return the number of cats over three years old that ");
    System.out.println("have claws.");
    System.out.println();

    Cat myCat1 = new Cat();


    Scanner input = new Scanner(System.in);

    myCat1.writeOutput();


    System.out.println("Please enter the cats name:" );
    String name = input.next();
    System.out.println();

    System.out.println("Please enter the cats age:" );
    int age = input.nextInt();
    System.out.println();

    System.out.println("Please enter the cats weight in pounds:" );
    int weight = input.nextInt();
    System.out.println();

    System.out.println("Please enter the cats breed:" );
    String breed = input.next();
    System.out.println();

    System.out.println("Please enter true if cat is declawed or ");
    System.out.println("false if cat is has claws." );
    boolean declawed = input.nextBoolean();
    System.out.println();


    System.out.println();
    System.out.println();
    System.out.println("My updated records now say:" );
    System.out.println();
    System.out.println("Enter the name of Cat " + name);
    System.out.println();
    System.out.println("Enter the age of Cat : " + age);
    System.out.println();
    System.out.println("Enter the weight of Cat: " + weight);
    System.out.println();
    System.out.println("Enter the breed of Cat : " + breed);
    System.out.println();
    System.out.println("Does the cat have claws? True or False: " + declawed);
    System.out.println();



    if (age >3 && declawed == false)

    {
    System.out.println("The following are the names and ages");
    System.out.println("of all cats entered over 3 years in age");
    System.out.println("and that have not been declawed");
    System.out.println("Name: "+ name);
    System.out.println("Age: "+age);


    }

    } /*end of main method*/
    } /* End of Cat class definition.*/

  2. #2
    Join Date
    Sep 2004
    Posts
    59
    Where is the cat class?
    My own java video tutorials, feel free to watch it.
    http://www.engineeringserver.com/for...amming-b311.0/ Need java help? For beginning and intermediate java coders!

  3. #3
    Join Date
    Mar 2007
    Posts
    2

    My Cat class

    import java.lang.*;
    import java.util.*;

    public class Cat extends PetRecord

    {
    public String breed,newBreed;
    public boolean declawed;
    public Cat()
    {
    super();
    breed = "None";
    declawed = false;
    }

    public Cat(String initialName, int initialAge, double initialWeight, String

    newBreed, boolean declawed)
    {
    super(initialName, initialAge, initialWeight);
    this.newBreed = newBreed;
    this.declawed = declawed;
    }

    public void reset(String nextName, int nextAge, double nextWeight, String nextBreed,

    boolean nextDeclawed)
    {
    super.set(nextName, nextAge , nextWeight);
    breed = newBreed;
    declawed = nextDeclawed;
    }
    public String getBreed()
    {
    return breed;
    }

    public void setBreed(String nextBreed)
    {
    breed = nextBreed;
    }

    public boolean getDeclawed()
    {
    return declawed;
    }

    public void setDeclawed(boolean nextDeclawed)
    {
    declawed = nextDeclawed;
    }
    public void Function()
    {
    if((getAge()>3) && (declawed == false))

    {
    System.out.println("The following are the names and ages");
    System.out.println("of all cats entered over 3 years in age");
    System.out.println("and that have not been declawed");
    System.out.println("Name: "+ name);
    System.out.println("Age: "+age);
    }
    else
    {
    System.out.println("You have entered no cats that fit the output criteria");
    }
    }

    }

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You are hard coding the creation of only one cat. To fix this you need to put the creation of cat instances into a loop: reading the data from System.in, accumulating the data, creating a cat instance. It would be a good idea to have the cats in a cats array or a cats vector or a cats arrayList ... you would not need to explicitly name the instances, just refer to them as cats[0], cats[1], and cats[2] ...

Similar Threads

  1. For Loop vs. While Loop
    By javatier in forum Java
    Replies: 16
    Last Post: 02-18-2011, 12:48 PM
  2. Do ... Loop Help
    By Chris Yard in forum VB Classic
    Replies: 4
    Last Post: 02-27-2007, 05:01 AM
  3. cout problem within While loop
    By LabAid_RRW in forum C++
    Replies: 7
    Last Post: 07-23-2006, 01:50 AM
  4. Replies: 2
    Last Post: 03-16-2006, 07:39 PM
  5. Problem with a loop
    By salvinger in forum VB Classic
    Replies: 0
    Last Post: 05-07-2005, 02:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center