Results 1 to 13 of 13
  1. #1
    Aceflare0 is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default [Solved] Looping for correct input

    It took me a while to realize how to do it, but thanks to the help of you guys, I got it working with this code. :) (I omitted the rest of my program)

    Java Code:
    import java.util.Scanner;
    
    
    class ThisThingIMade {
    	public static void main(String[] args) {
    	System.out.println("Would you like to play? (Yes/No)");
    	Scanner scanner = new Scanner(System.in);
    
    	String yesNo = "Derp";
    	do {
    	yesNo = scanner.nextLine();
    	if(yesNo.equalsIgnoreCase("yes")) {
    	System.out.println("What is 7 x 6?");
    	} else if(yesNo.equalsIgnoreCase("no")) {
    	System.out.println("Giving up already? Panzy...");
    	System.out.println("Press enter to exit.");  
    	try { System.in.read(); }
    	catch(Exception e) {} 
    	return;
    	} else {
    	System.out.println("Please answer yes or no, this program isn't a genius.");
    	yesNo = "Derp";
    }
    	} while(!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no"));
    Last edited by Aceflare0; 02-16-2013 at 06:02 AM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to make this work?

    You need a loop. My suggestion would be to use the do-while loop.

    Read the tutorial and try it again :-)

  3. #3
    Aceflare0 is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to make this work?

    Edit: I used do-while and it loops like I want it now, but when you put in yes after you've put an incompatible answer it goes to the next block without sending the next question.


    Console:
    Java Code:
    C:\Documents and Settings\Owner\Desktop\Will's game\Random stuff>java ThisThingI
    Made
    Would you like to play? (Yes/No)
    huh
    Please answer yes or no, this program isn't a genius.
    yes
    42
    Woah, ye beat meh.

    Java Code:
    import java.util.Scanner;
    
    
    class ThisThingIMade {
    	public static void main(String[] args) {
    	System.out.println("Would you like to play? (Yes/No)");
    	Scanner scanner = new Scanner(System.in);
    
    	String yesNo = scanner.next();
    	do {
    	if(yesNo.equalsIgnoreCase("yes")) {
    	System.out.println("What is 7 x 6?");
    	} else if(yesNo.equalsIgnoreCase("no")) {
    	System.out.println("Giving up already? Panzy...");
    	} else {
    	System.out.println("Please answer yes or no, this program isn't a genius.");
    	yesNo = scanner.next();
    }
    	} while(!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no"));
    
    	double fortyTwo = scanner.nextDouble();
    	if(fortyTwo == 42) {
    	System.out.println("Woah, ye beat meh.");
    	} else {
    	System.out.println("YOU LOSE!");
    		}
    	}
    }
    Last edited by Aceflare0; 02-15-2013 at 08:41 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: How to make this work?

    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    382
    Rep Power
    5

    Default Re: How to make this work?

    Hi Aceflare0, welcome to the forums.

    The reason you are not seeing the next question is the way the do while loop works. The loop validates the condition after executing the code within it, unlike a do loop which validates the condition before executing the code.

    At the end of the loop you ask the user to put their input in again. The do while loop then uses this input to validate the condition.

    Post back if you require assistance correcting this issue.

    Regards.

  6. #6
    Aceflare0 is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to make this work?

    Yeah I'm just completely lost now >_<; if the program knows the condition has just then been filled, why does it not then execute the condition's code? How do I make it do it?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: How to make this work?

    Java Code:
    String yesNo = scanner.next();
    do {
    ... handle 'yes' or 'no' ...
       } else {
          System.out.println("Please answer yes or no, this program isn't a genius.");
          yesNo = scanner.next();
       }
    } while(!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no"));
    double fortyTwo = scanner.nextDouble();
    if (fortyTwo == 42) {
       System.out.println("Woah, ye beat meh.");
    } else {
       System.out.println("YOU LOSE!");
    }
    OK, this is the flow it goes through when you input an incorrect value followed by a correct value.
    It hits the 'while' with a correct value, so exits, which you don't want.

    Since this is doWhile, put the initial read of yes/no inside the do-while, and remove the read at the end.
    That will mean it will read only inside the loop and will not read again at the end, which is what's causing your problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: How to make this work?

    Use try-catch.It will catch any error and you can restart the program by running the method again..
    link
    link_2

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: How to make this work?

    Try/catch is utterly pointless here.
    The OP has almost the correct code, they just need to move where they read the input.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: How to make this work?

    Quote Originally Posted by Tolls View Post
    Try/catch is utterly pointless here.
    The OP has almost the correct code, they just need to move where they read the input.
    OP?? whats that?

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    980
    Rep Power
    2

    Default Re: How to make this work?

    Original Poster

  12. #12
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: How to make this work?

    Quote Originally Posted by SurfMan View Post
    Original Poster
    okay mate.

  13. #13
    Aceflare0 is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to make this work?

    Thanks for the help everyone, I got it to work how I wanted and I'm nearly finished with the program, just adding more questions. :3

Similar Threads

  1. New to java stop need help stop
    By Skrap09 in forum New To Java
    Replies: 3
    Last Post: 09-17-2011, 12:45 PM
  2. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •