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 19 of 19

Thread: Prompt user to repeat

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Prompt user to repeat

    Hi,

    I have a program that loops ten times and asks a question each time with an appropriate response for the user's input. My problem is i'm trying to get the program to ask at the end, "Do you want to go again? Type "Yes" or "No"."

    Below isn't my actual program, I made a mock up because the actual one is for college and I don't want to get into trouble.

    class Mock  
    {  
        public static void main(String[] args)  
        {  
     
     
        int ans= 0;  
        int loop =0;  
        int i=1;  
        int end= 0;  
     
        do  
    {  
     
        System.out.println("Do u like number "+i)  
        ;i++;  
        ans = EasyIn.getInt();  
     
     
     
        if (ans == 1)  
    {  
        System.out.println("Good");  
    }  
     
     
     
        else if (ans == 2)  
    {  
        System.out.println("Thats OK!");  
    }  
     
     
        else  
    {  
        --i; --loop;  
        System.out.println("1 or 2 only!!");  
        System.out.println();  
    }  
        {  
        loop++;  
        }  
    }  
        while (loop <10);  
        }  
    }

    I added this to the end but i'm sure that its completely wrong and its missing stuff also.

    	System.out.println("Would you like to continue? '1 = yes' '2 = no'")
    	end = EasyIn.getInt
     
    	if
    	{
    		(end == 1)
    		//Need to go back to the start of the loop.
    	}
              else
    	{
    		System.out.println("Bye")
    	}
    	while (loop <10);
    }


    PS: I was using int's just to make the example easier.
    Thanks you for any help.
    Last edited by Callcollect; November 24th, 2011 at 05:59 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prompt user to repeat

    Use continue where you want your loop to iterate again but don't forget to iterate the value before moving it to continue statement.
    continue;

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Prompt user to repeat

    Quote Originally Posted by Mr.777 View Post
    Use continue where you want your loop to iterate again but don't forget to iterate the value before moving it to continue statement.
    continue;
    I looked up some tutorials with the continue statement but I cant seem to see how it would work in this code?

    class Mock  
    {  
        public static void main(String[] args)  
        {  
     
     
        int ans= 0;  
        int loop =0;  
        int i=1;  
        int end= 0;  
     
        do  
    {  
     
        System.out.println("Do u like number "+i)  
        ;i++;  
        ans = EasyIn.getInt();  
     
     
     
        if (ans == 1)  
    {  
        System.out.println("Good");  
    }  
     
     
     
        else if (ans == 2)  
    {  
        System.out.println("Thats OK!");  
    }  
     
     
        else  
    {  
        --i; --loop;  
        System.out.println("1 or 2 only!!");  
        System.out.println();  
    }  
        {  
        loop++;  
        }  
    }  
        while (loop <10);  
        }  
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Prompt user to repeat

    You will need nested loops
    loop while user wants to keep going {
        inner loop ten times {
            ask question
            ....
        }
        ask if user wants to try again
    }
    I don't see how continue has anything to do with the problem.
    Improving the world one idiot at a time!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prompt user to repeat

    Quote Originally Posted by Junky View Post
    I don't see how continue has anything to do with the problem.
    The OP seemed to say that he needs to continue if certain condition occur and continue processing further statements in other case.

  6. #6
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Prompt user to repeat

    I would say add an if statement right before the end of the do-while loop.

    if(loop == 9)
    {
         System.out.println("Do you want to go again?\nYes or No";
         Scanner scan = new Scanner(System.in);
         String i = scan.nextLine();
         if(i.equalsIgnoreCase("Yes"))
              loop = 0;
    }

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prompt user to repeat

    Quote Originally Posted by that_guy View Post
    I would say add an if statement right before the end of the do-while loop.

    if(loop == 9)
    {
         System.out.println("Do you want to go again?\nYes or No";
         Scanner scan = new Scanner(System.in);
         String i = scan.nextLine();
         if(i.equalsIgnoreCase("Yes"))
              loop = 0;
    }
    Spoon feeding is not allowed.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Prompt user to repeat

    Quote Originally Posted by Mr.777 View Post
    The OP seemed to say that he needs to continue if certain condition occur and continue processing further statements in other case.
    A while loop is going to continue anyway if you don't change the condition so using continue is totally pointless. Did you read my pseudocode? It achieves what the OP wants without a continue statement.
    Improving the world one idiot at a time!

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prompt user to repeat

    Quote Originally Posted by Junky View Post
    A while loop is going to continue anyway if you don't change the condition so using continue is totally pointless. Did you read my pseudocode? It achieves what the OP wants without a continue statement.
    I didn't disagree with your pseudocode or the solution you provided to the OP. What i told you is, i thought OP needs to execute number of statements if certain condition fulfils else OP doesn't want to execute (in a loop), that's why i pointed towards continue statement. May be lack of understanding of the problem or whatever.

  10. #10
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Prompt user to repeat

    hmm If your new to loops, Stick to simple code such as JOptionPane

    for example a string varible getting data from the optionpane

    ... 
     
    _strString = JOptionPane.ShowInputDialog(null, "Would you like to restart?");
    _strString.toLowerCase();
     
     
    if(_strString.equals("yes"))
    {
        // enter code if the user enters yes
    }
    else if (_strString.equals("no"))
    {
         // Enter code if the user enters no
    }
    else
    {
         // Enter code if the user enters invalid choice
    }
     
    ...

    Ignore my case sensitive if its wrong xD..

    Simple but works and easily understood.

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Prompt user to repeat

    Quote Originally Posted by Mr.777 View Post
    i thought .
    Well you thought wrong and your suggestion to use a continue statement was wrong. With you continuing to discuss the matter only leads me to think that you are trying to justify your advice. We all makes mistakes. When you get corrected accept it, learn from it and move on.
    Improving the world one idiot at a time!

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prompt user to repeat

    Quote Originally Posted by Junky View Post
    Well you thought wrong and your suggestion to use a continue statement was wrong. With you continuing to discuss the matter only leads me to think that you are trying to justify your advice. We all makes mistakes. When you get corrected accept it, learn from it and move on.
    Junky stop being such a jerk to me. See this example
    int i=10;
    while(i<100){
    if(i%2==0){
    i++;
    continue;
    }else{
    System.out.println(i);
    i++
    }
    So, what do you say about the usage of continue here?
    I've never denied accepting my fault and i am not trying to justify my advice. WHat i've tried to tell you is the understanding what i got from OP's post. Hope you understand and better stay on the topic rather teaching me about what i thought or not.
    Sorry to be so hard.

  13. #13
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Prompt user to repeat

    Quote Originally Posted by Mr.777 View Post
    So, what do you say about the usage of continue here?
    I'd say it is poorly formatted, has 2 syntax errors and the same result can be achieved by:
    int i=10;
    while(i<100){
        if(i%2 != 0) {
            System.out.println(i);
        }
        i++;
    }
    I'm not saying that "continue" has no purpose in coding. What I am saying is that your suggestion to the OP to use continue was wrong. For their situation continue was not needed. Therefore you offered bad advice and I simply corrected you but apparently you got your panties in a bunch. So why is offering bad advice wrong? Because the OP and anyone else who reads your post goes away thinking they HAVE to use continue and might spend wasted time and effort developing a solution that can be achieved in a much simpler way.
    Improving the world one idiot at a time!

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Prompt user to repeat

    An even better solution:
    int i = 11;
    while(i < 100) {
        System.out.println(i);
        i += 2;
    }
    Improving the world one idiot at a time!

  15. #15
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prompt user to repeat

    Quote Originally Posted by Junky View Post
    An even better solution:
    int i = 11;
    while(i < 100) {
        System.out.println(i);
        i += 2;
    }
    Will you mind giving me a break? I already told you, i adviced what i thought OP is asking about. Now, better stay away and stick to the topic rather scratching my butts.

  16. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Prompt user to repeat

    Hey you are the one who keeps trying to argue with me and giving me ammunition to fire up your backside. As I said earlier if you had just accepted the criticism and left at that this conversation would have ended 10 posts ago.
    Improving the world one idiot at a time!

  17. #17
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prompt user to repeat

    Quote Originally Posted by Junky View Post
    Hey you are the one who keeps trying to argue with me and giving me ammunition to fire up your backside. As I said earlier if you had just accepted the criticism and left at that this conversation would have ended 10 posts ago.
    First, i never got time to argue with the people who have nothing else to do. Second, i already told you that the advice given to OP by me was for some other scenario, what i thought and i told you earlier. Now, what do you want to prove? Either you are a Java legend? Grow up and get some life Junky. Better stop this converstaion here coz i am not gonna admit if i was wrong with the advice. Coz i advised about the scenario i understood. Yeah you can say that i misunderstood but still it's worth proving me wrong.

  18. #18
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Prompt user to repeat

    Quote Originally Posted by Mr.777 View Post
    First, i never got time to argue.
    Really? Your last five posts say otherwise.

    I'm sorry but how you can you justify giving incorrect advice simply because it would be correct if the OP asked a different question? That's crazy talk.
    Improving the world one idiot at a time!

  19. #19
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,097
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Prompt user to repeat

    This discussion is slowly becoming unbeneficial to the original topic at hand. Please leave it at this or if you must take it 'offline' through a private message or profile conversation - I would rather not have to lock this thread because of this.

Similar Threads

  1. One last question: How to get menu to repeat!
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 05:52 AM
  2. How to repeat a while loop?
    By BITmixit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 12th, 2011, 12:51 PM
  3. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM
  4. Wanting to repeat my program...
    By Shaybay92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 8th, 2011, 08:29 AM
  5. how do I keep a persistent prompt in JTextArea and allow user input
    By cazmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2010, 12:14 PM