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);
    }
}

Edited 4 Years Ago by peter_budo: Changed to discussion thread

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);
    }
}
This article has been dead for over six months. Start a new discussion instead.