Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Getting program to ask user input three times

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Getting program to ask user input three times

    Quote Originally Posted by Java_noob333 View Post
    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.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    Java_noob333 (February 23rd, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Getting program to ask user input three times

    Quote Originally Posted by Java_noob333 View Post
    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.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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)

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Getting program to ask user input three times

    Quote Originally Posted by Java_noob333 View Post
    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:

    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.,

    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.

Similar Threads

  1. user input program using BufferedReader..
    By mbalamula in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 22nd, 2013, 07:46 AM
  2. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  3. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  4. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  5. Counting How Many Times Program Was Excecuted
    By Override in forum Loops & Control Statements
    Replies: 2
    Last Post: October 30th, 2010, 05:11 PM