Reputation: 25
My code looks like this so far:
public class CatWorld {
public static void main (String[] args) {
Scanner getLine = new Scanner(System.in);
String userSays;
//ARRAY:
int [] CatArray;
CatArray = new int [5];
//ARRAY-POWERED LOOP:
for (int i=0; i < CatArray.length; i ++) {
//OPTIONAL PROMPT:
System.out.println ("Wow! A brand new cat! What's its name?");
//Mandatory below
userSays = getLine.next();
Cat babyCat = new Cat(userSays);
System.out.println ("The new cat's name is "
+ babyCat.getcatName() + "!");
}
}
}
And my constructor looks like this:
public class Cat {
String catName = "Billybob";
public Cat (String Name) { //Can also be birthName
catName = Name;
}
public String getcatName(){
return catName;
}
}
What happens when I run it is that it outputs right after I input the name. How would I go about outputting them all after the 5 name inputs?
Upvotes: 2
Views: 108
Reputation: 14413
You have to store your cat in the array you created. Then you loop and you show them in console.
Cat[] catArray = new Cat[5];
for (int i=0; i < catArray.length; i ++){
System.out.println ("Wow! A brand new cat! What's its name?");
userSays = getLine.next();
catArray[i] = new Cat(userSays);
}
Then you loop again:
for (int i=0; i < catArray.length; i ++){
System.out.println ("The new cat's name is "
+catArray[i].getcatName() + "!");
}
By the way, you should follow java code conventions variable's name starts with lower-case.
Upvotes: 0
Reputation: 13483
Tested and works
Change your code in you main
method to this:
Scanner getLine = new Scanner(System.in);
String userSays;
Cat[] catList = new Cat[5]; // create array of 5 cats
int catCount = 0;
// loop to get all the user input for the cats
for (Cat cat : catList) // for each cat in cat array (5 cats)
{
System.out.println("Wow! A brand new cat! What's its name?");
userSays = getLine.next(); // get the cat name
catList[catCount] = new Cat(userSays); // set this cat in the cat array to be
// the user input
catCount++; // + the catCount, so the next cat in the cat array is focused on
}
// loop to display all of the cats back to the console
for (Cat cat : catList) // for each cat in the cat array
{
// display the cat's name in this iteration of the cat array
System.out.println ("The new cat's name is " + cat.getcatName() + "!");
}
Upvotes: 1
Reputation: 5612
You need to store the Cat
s somehow.
List<Cat> catList = new ArrayList<Cat>();
// Ask for cat names, and insert cat objects into list
then at the end,
for (Cat cat : catList) {
// print whatever you want about the cats.
}
Upvotes: 1
Reputation: 209112
int [] CatArray;
Cat[] catName = new String[5]; // Get cat names
CatArray = new int [5];
//ARRAY-POWERED LOOP:
for (int i=0; i < CatArray.length; i ++){
//OPTIONAL PROMPT:
System.out.println ("Wow! A brand new cat! What's its name?");
//Mandatory below
userSays = getLine.next();
catName[i] = new Cat(userSays);
}
for(int i = 0; i < catName.size; i++) {
System.out.println ("The new cat's name is "
catName[i].getcatName() + "!");
}
Upvotes: 0