0

I have to write a program that asks the user for his name, address and phone number. When the data is entered the program shall print the data and ask the user to verify the data by entering yes or no. This process shall be repeated until the user is satisfied and answers yes to the question.

Now, at this moment I am able to pop-up a single prompt (in my case asking only the user's name). But what if I want to add multiple question (i.e. asking address and telephone number) and happen the same thing? How could I do that?

My code:

    package userinfo;

import java.util.Scanner;
import sun.security.krb5.SCDynamicStoreConfig;

public class UserInfo {

    public static void main(String[] args) {

        String varify;
        String yes = "yes";
        String no = "no";

        Scanner input = new Scanner(System.in);
        System.out.println("Enter your name: ");
        String name = input.next();
        System.out.println("Your input was: "+name);
        System.out.println("Varify by yes or no: ");

        while (true) {
            varify = input.next();

            if (varify.equalsIgnoreCase(yes)) {
                System.out.println("Varified! Your name is: " + name);

            } else if (varify.equalsIgnoreCase(no)) {
                System.out.println("Type your name again: ");

            }

        }

    }

}

2 Answers 2

1

You can extract this code to a method:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String userName = readFieldAndVerify(input, "Enter your name: ");
    String userAddress = readFieldAndVerify(input, "Enter your address: ");
    String userPhoneNumber = readFieldAndVerify(input, "Enter your phone number: ");
}

private static String readFieldAndVerify(Scanner input, String prompt) {
    while (true) {
        System.out.print(prompt);
        String field = input.next();

        System.out.println("Are you sure (yes / no)?");
        String verify = input.next();

        if (verify.equalsIgnoreCase("yes")) {
            System.out.println("Verified!");
            return field;
        } else {
            System.out.println("Canceled");
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

much better than my method for multiple questions. assumed OP just wanted a simple example.
alaster, this is what I wanted. can't wait to accept your answer.
1

EDIT Added logic for more questions... Expand it in similar fashion for everything you need. You could expand this code into a single method as well so you avoid code replication. Check answer from user alaster for an example.

Try this. It will store the name variable in case you want to use it further.

We use a boolean to keep asking the user to input his name until he validates it.

Of course, you can still use while(true) and then break if the name is valid, but I prefer this method since the code is more clear and easier to understand.

private static boolean isVerified(String verify) {
    if (verify.equalsIgnoreCase("yes")) {
        return true;
    } else if (verify.equalsIgnoreCase("no")) {
        return false;
    } else
        return false;
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    boolean validName = false;
    boolean validTelephoneNo = false;
    boolean validAddress = false;
    String name="";
    String telephoneNo="";
    String address="";

    while (!validName) {
        System.out.print("Enter your name: ");
        name = input.next();

        System.out.println("Are you sure your name is " + name + "?");
        final String verify = input.next();

        if (isVerified(verify)) {
            System.out.println("Verified! Your name is: " + name);
            validName = true;

        } else {
            System.out.println("Not verified! Please type your name again.");
        }
    }

    while (!validTelephoneNo) {
        System.out.print("Enter your telephone nummber: ");
        telephoneNo = input.next();

        System.out.println("Are you sure your telephone number is " + telephoneNo + "?");
        final String verify = input.next();

        if (isVerified(verify)) {
            System.out.println("Verified! Your telephone number is: " + telephoneNo);
            validTelephoneNo = true;
        }

        else {
            System.out.println("Not verified! Please type your telephone number again.");
        }
    }

    while (!validAddress) {
        System.out.print("Enter your address: ");
        address = input.next();

        System.out.println("Are you sure your address is " + address + "?");
        final String verify = input.next();

        if (isVerified(verify)) {
            System.out.println("Verified! Your address is: " + address);
            validAddress = true;
        }

        else {
            System.out.println("Not verified! Please type your address again.");
        }
    }

    System.out.println("Done, here is your info:");
    System.out.println("Name: " + name);
    System.out.println("Telephone Number: "+telephoneNo);
    System.out.println("Address: "+address);
}

3 Comments

But I want to know how to add another question. That means when the user varified by YES, the program should print the confirmation name and ask the next question (here, the telephone number/address). And again asking for varification..
MihaiC, I appreciate your solution. its also a good one. But since I tried the alester's one first and his code is simpler too. I accepeted his answer. Anyway thanks for your time and effort, I will save your suggestion for my future use.
@JabirAlFatah no problem :) it is a better solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.