Main Method

I would like to know how to rewrite this main method so that it can read 3 or more inputs. It only reads one input at the moment.

import java.util.*;

public class Java

{

public static void main(String[] args)

{

Cat myPet1 = new Cat ("bob", 3, 7.623, "burmese", true );

Cat myPet2 = new Cat ("kool", 2, 4.333, "house", true );

myPet1 = myPet2;

for (int i = 0; i < 2; i++)

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter name of cat:");

String correctName = keyboard.nextLine( );

System.out.println("Enter age of cat:");

int correctAge = keyboard.nextInt( );

System.out.println("Enter weight of cat:");

double correctWeight = keyboard.nextDouble( );

System.out.println("Enter the breed of cat:");

String correctBreed = keyboard.next( );

System.out.println("Does the cat have claws? True or False?:");

boolean haveclaws = keyboard.nextBoolean( );

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

myPet1.set(correctName, correctAge, correctWeight);

myPet1.set(haveclaws);

myPet1.set(correctBreed);

}

;

if ((myPet1.getAge() > 3 ) && ( myPet1.getDeClawed()))

{

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

System.out.println("Name: " + myPet1.getName() );

System.out.println("Age: " + myPet1.getAge() +" years old");

}

}

}

[1458 byte] By [zebra40a] at [2007-11-27 9:20:24]
# 1

> I would like to know how to rewrite this main method so that it can read 3 or more

> inputs.

The content of the for loop could be moved to a new method that creates and returns a new Cat. The for loop itself could then be replaced with a loop that keeps adding the newly created cats to a collection (until the user indicates that there are no more, or however you wish to limit the process.)

Later when you want to find cats over 3 with claws you would examine the contents of the collection.

pbrockway2a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 2

Can I just point out that this has nothing at all to do with "main method", other than you happen to have written your code in the main method. The question would remain the same if you wrote the code in another method. Having misleading subject lines can hinder your chances of getting an answer

georgemca at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 3
I am sorry , but I am still not clear on what you are trying to explain.The current loop only adds and displays one cat, it totally ignores the second and third.
zebra40a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 4

To spell some things out:

1) Have a Cat array of 3 Cats.

2) Have a for loop that contains a method to allow you to update your Cat array:

for (i = 0; i < 3; i++)

{

myCatArray[i] = makeNewCat();

}

3) Create a method (here called makeNewCat()) that accepts user input, creates a new Cat and stuffs the input into the new cat and returns this Cat variable.

petes1234a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks I will try that.
zebra40a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 6

Correction:

To be somewhat cleaner and safer, the loop should look like:

for (i = 0; i < myCatArray.length; i++)

{

myCatArray[i] = makeNewCat();

}

You may have an array of 3 cats this week but decide to change it to seven next week. So without this change, this is a bug waiting to happen.

petes1234a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 7

The advice has been great thus far, but I am getting all sorts of errors.

I have completed these steps:

created the method in my derived class.

declared the var in my main method , created the loop as suggested and now I am getting these error messages even after I call this method

public String makeNewCat()

{

return makeNewCat;

}

I now get this error:

cannot find symbol

symbol : method makeNewCat()

location: class week3_4Prog

myCatArray = makeNewCat();

^

zebra40a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 8

> created the method in my derived class.

The suggestion wasn't to add any methods to any other class. I'm not sure what you mean by "my derived class", but makeNew Cat() should go in the class you posted.

You currently have this method returning a String. Why? As was suggested above this method should create and return a Cat.

As far as the body of this method goes, it's not clear what "return makeNewCat;" is supposed to do. Again as suggested a couple of times above, the method should simply repeat what you were doing in the for loop.

Perhaps you should post the full code along with the error messages. Use the code tags (Click on "Formatting tips" near the text area where you enter your post). But also comment the code or otherwise explain what you intend each method to do.

pbrockway2a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 9

Please be patient I am trying to understand the instructions. I have a base class and a derived class and a main method. The main method that I have posted is supposed to call methods from these classes. I am not sure how to create methods in my main method

Message was edited by:

zebra40

zebra40a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 10

This might help:import java.util.*;

public class Java

{

public static void main(String[] args)

{

// (1) Have a Cat array of 3 Cats

/* Declare the array here */

// (2) Have a for loop that contains a method to allow you to

//update your Cat array:

for (int i = 0; i < myCatArray.length; i++)

{

myCatArray[i] = makeNewCat();

}

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

for (int i = 0; i < myCatArray.length; i++)

{

Cat toTest = myCatArray[i];

if(toTest.getAge() > 3 ) && toTest.getDeClawed())

{

System.out.println("Name: " + toTest.getName() );

System.out.println("Age: " + toTest.getAge() +" years old");

}

}

}

// (3) Create a method (here called makeNewCat()) that accepts user

//input, creates a new Cat and stuffs the input into the new cat and

//returns this Cat variable.

static Cat makeNewCat()

{

/* Use code similar to what you had in the for loop to get information */

/* Use the Cat class constructor (and possibly setters) to create the Cat */

/* Return the Cat you have created */

}

}

pbrockway2a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...
# 11
Thank you for your time.
zebra40a at 2007-7-12 22:13:30 > top of Java-index,Java Essentials,New To Java...