Hi all
Here is my snippet

for(int i = 1;;i++){                            
                System.out.println("Please enter the name:");                   
                name = input.nextLine();
                System.out.println("Please enter the email:");
                email = input.nextLine();
                if(!email.matches(EMAIL_PATTERN)){                  
                    System.out.println("Please enter a valid email.\nThe details are not saved please start from the beginning.");
                    i--;
//                  break;
                    continue;
                }
                else
                {
                    printwriter.println("|"+i+")\t|"+name+"\t|"+email+"\t|");
                    printwriter.println("+-----\t+-----\t+-----\t+");
                }                       

                if(i>=1){                   
                    System.out.println("Do you wish to continue ?(y/n)");
                    String quit = input.nextLine();     
                    if(quit.equalsIgnoreCase("n"))break;
                    else if (quit.equalsIgnoreCase("y")){
                        continue;

                    }
                    else
                    {
                        System.out.println("Please enter either 'y' or 'n'");   
                        //how do I pass the control back to the if(i >= 1) ?
                    }

                }               
            }

So I am using the scanner to read the name and email from the standard input, for the first time it will not ask the user whether to contiue or quit, from the second time it will ask the user whether he/she wants. So he/she is left with two choices to choose (i.e) y/n, the code works for these two cases, what if the user enters something other than y/n ? for example what if he enters dummy? so for that situation I'd written that else part, but now after printing that message it does nothing, I would like to transfer the control to this if(i >= 1).... How would I do that ?

Thanks
Varun Krishna. P

Recommended Answers

All 6 Replies

Put a while() loop around it.

Wrap it in another loop. If the user enter's an incorrect answer (not 'y' or 'n'), thne the loop continues. If the user enter's a correct answer, the loop breaks.

@Schol-R-LEA
Do you mean change the if () in the line no 18 ?

@Hiroshe I have changed the code from line 18 - 32 as follows

B:for(;;){              
    if(i>=1){                   
        System.out.println("Do you wish to continue ?(y/n)");
        String quit = input.nextLine();     
        if(quit.equalsIgnoreCase("n"))break;
        else if (quit.equalsIgnoreCase("y")){
            continue A;
            //A:for(int i = 1;;i++){ 

        }
        else
            {
                System.out.println("Please enter either 'y' or 'n'");
                continue B;
            }

    }
}

But now if I hit n/N the first for loop is getting called again. Why is the control getting passed to the for loop (at line 1 of my original post) ?

Because you're breaking the inner loop, not the outter loop. Name the outerloop, and then use if (quit.equalsIgnoreCase("n")) break outterloop; (probably with a better name) instead.

Yeah, thanks if(quit.equalsIgnoreCase("n"))break A; it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.