How do you write an input validation loop that compares strings? My attempt at the code is included.
Thank You,
Hank

Question 4.6. Write an input validation loop that asks the user to enter “Yes” or “No”.

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.Scanner;


/**
CIS-151, 
Input Validation Loop
*/

public class Checkpoint4_6  // Name the file.


{
    public static void main(String[] args)
    {

        DecimalFormat formatter = new DecimalFormat("#0.00");       // Create decimal format object.

        int number; // number
        String input; // hold user input
        char ch; // 

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter 'Yes' 'No'.");
        input = keyboard.nextLine();
        //ch = input.charAt(0);
         while (input.equals("Yes"))// && (input.equals("No"))
         {
            System.out.println("Enter 'y', 'Y', 'n' or 'N'.");
            input = keyboard.nextLine();
            //ch = input.charAt(0);
            //System.out.println(input);
         }  



        System.exit(0);
    }
}

I'm not sure what exactly you're looking for, but does this help?

-Bill

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * CIS-151, Input Validation Loop
 */
public class Checkpoint4_6 // Name the file.
{

    public static void main(String[] args) {
        DecimalFormat formatter = new DecimalFormat("#0.00"); // Create decimal format object.
        int number; // number
        String input; // hold user input
        char ch; //
        Scanner keyboard = new Scanner(System.in);

        while (true) {
            System.out.println("Enter 'Yes', 'No' or 'Quit'");
            input = keyboard.nextLine();

            if (input.equals("Yes")) {
                System.out.println("You entered Yes.");
            } else if (input.equals("No")) {
                System.out.println("You entered No.");
            } else if (input.equals("Quit")) {
                System.out.println("You entered Quit. Stopping program...");
                break;
            } else {
                System.out.println("You must enter 'Yes', 'No' or 'Quit'");
            }    
        }
        System.exit(0);
    }
}
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.