Getting program to ask user input three times
The program is supposed to get three cats info and display it. I thought that java would ask the input questions until all the variables were met. but it only asks once and then displays it three times. Just looking for a point in the right direction.
Re: Getting program to ask user input three times
Quote:
Originally Posted by
Java_noob333
The program is supposed to get three cats info and display it. I thought that java would ask the input questions until all the variables were met. but it only asks once and then displays it three times. Just looking for a point in the right direction.
The code will only do what you tell it to. How many times in your code above do you have a println asking for the cat's name followed by a call to input.next()? Once. So that's the number of times that this code will be executed, no more no less. If you want the question to be asked 3 times, your code must ask it 3 times. The easiest way to do this is to use a for loop and an array of Cat, but if you haven't reached that step, then you will need to println the question 3 times, accept the answer 3 times, and put the answer into 3 Cat objects.
Re: Getting program to ask user input three times
Yea we havent got to arrays. When I did code it to ask three times and kept saying the variable had already been assigned.
Re: Getting program to ask user input three times
Quote:
Originally Posted by
Java_noob333
Yea we havent got to arrays. When I did code it to ask three times and kept saying the variable had already been assigned.
It's hard to know what's wrong without seeing your code and the actual error messages as we're notoriously bad at guessing at these things sight unseen and at reading minds.
So please post your latest code attempt as well as any complete error messages that you may get from the compiler or exception messages that you might get from the JVM.
Re: Getting program to ask user input three times
its not throwing any errors any more. I just cannot figure out why it wont run the commands three times for each of the three variables (cat1,cat2,cat3)
Re: Getting program to ask user input three times
Quote:
Originally Posted by
Java_noob333
its not throwing any errors any more. I just cannot figure out why it wont run the commands three times for each of the three variables (cat1,cat2,cat3)
It's exactly as I explained before: you only ask the user for each specific bit of input once. Programs are dumb, they can't read minds, and they'll only do what you tell them to. For instance if I want the user to multiply two numbers, this code won't work:
Code :
Scanner scan = new Scanner();
System.out.println("Enter a number: ");
int x = scan.nextInt();
int y = x;
System.out.println("The answer is: " + (x * y));
Even though I have two int variables above, I only ask for a numeric value once, and so I use the same numeric value for both numbers (kind of what you're doing with your current code.
To get two distinct numbers above, I have to ask the same question and retrieve the same data two times (more if I want to get more numbers). i.e.,
Code :
Scanner scan = new Scanner();
System.out.println("Enter a number: ");
int x = scan.nextInt();
// here I ask a *second* time
System.out.println("Enter a number: ");
int y = scan.nextInt();
System.out.println("The answer is: " + (x * y));
You have to do the same thing -- you have to write code that asks for your Cat data 3 times. Again the easiest way to do this is to create an array of Cat[] cats = new Cat[3], and then use a for loop that loops 3 times, getting data from the user each time, creating a new Cat in the loop, and filling in the respective Cat array item with this new Cat that has new data.