Validate the input using a while loop

Mr.Brooks

I doing a little practice on Computer Science because when I leave the military I want to start taking classes on the basics of java. I'm a little stuck on this question i was wondering if i can get some assistance.

a program that allows the user to enter a character. The only valid values are 'A', 'M', and 'S'. Validate the input using a while loop so that if the user enters any value other than one of those 3 characters, an error message is displayed and the user is prompted for another value. Once the user has finally entered valid data, print the character they entered back to the screen.

mary_jane

You can look into this basic example

import java.util.Scanner;
public class Read {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean isCheck = true;
        while (isCheck) {
            String str = sc.next();
            switch (str) {
                case "A":
                    System.out.println("A");
                    isCheck = false;
                    break;
                case "M":
                    System.out.println("M");
                    isCheck = false;
                    break;
                case "S":
                    System.out.println("S");
                    isCheck = false;
                    break;
                default:
                    System.out.println("Not Valid : Enter next");
                    isCheck = true;

            }
        }

    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to validate input string using while loop

From Dev

While loop to validate input is a number in C?

From Dev

Python 3 - 'While loop' to validate that the user input is between 2 numbers

From Dev

how to validate the input while entering the data using Jquery event?

From Dev

user input using while loop to store data

From Dev

Using while loop to write user input to a file

From Dev

Break while loop using user input

From Dev

Creating folders from user input using while or do while loop

From Dev

How to input vector members in the console while using a while loop?

From Javascript

While Loop Input Issue

From Dev

Providing input for while loop

From Dev

While loop user input?

From Dev

Using while loop to check input but it runs twice from the second time

From Dev

Using while loop to make user input only an existing user

From Dev

Using || in while loop makes it take too many input values

From Java

'while' infinite loop using scanner input and pos/neg numbers

From Java

How to close a while loop using user input for doubles

From Dev

How to close my while loop using -1 input?

From Java

using a while loop for user to input multiple int and find the max and min

From Dev

Get user input without blocking while loop using threads in C

From Dev

How to read from two input files using while loop

From Dev

getting infinite loop in c while using eof for specific value of input

From Dev

Using a while loop to check for input errors within a switch case

From Dev

Move forward not using unity Input system (while loop error)

From Dev

Using scanf with a while loop to input two separate strings

From Dev

Java - losing first user input using a while loop

From Dev

Java: using a string to stop a while loop and it is consumed by following scanner input

From Dev

While loop using a counter utilizing user's input of an integer

From Dev

why getline ignoring first character of input while using in loop?

Related Related

  1. 1

    How to validate input string using while loop

  2. 2

    While loop to validate input is a number in C?

  3. 3

    Python 3 - 'While loop' to validate that the user input is between 2 numbers

  4. 4

    how to validate the input while entering the data using Jquery event?

  5. 5

    user input using while loop to store data

  6. 6

    Using while loop to write user input to a file

  7. 7

    Break while loop using user input

  8. 8

    Creating folders from user input using while or do while loop

  9. 9

    How to input vector members in the console while using a while loop?

  10. 10

    While Loop Input Issue

  11. 11

    Providing input for while loop

  12. 12

    While loop user input?

  13. 13

    Using while loop to check input but it runs twice from the second time

  14. 14

    Using while loop to make user input only an existing user

  15. 15

    Using || in while loop makes it take too many input values

  16. 16

    'while' infinite loop using scanner input and pos/neg numbers

  17. 17

    How to close a while loop using user input for doubles

  18. 18

    How to close my while loop using -1 input?

  19. 19

    using a while loop for user to input multiple int and find the max and min

  20. 20

    Get user input without blocking while loop using threads in C

  21. 21

    How to read from two input files using while loop

  22. 22

    getting infinite loop in c while using eof for specific value of input

  23. 23

    Using a while loop to check for input errors within a switch case

  24. 24

    Move forward not using unity Input system (while loop error)

  25. 25

    Using scanf with a while loop to input two separate strings

  26. 26

    Java - losing first user input using a while loop

  27. 27

    Java: using a string to stop a while loop and it is consumed by following scanner input

  28. 28

    While loop using a counter utilizing user's input of an integer

  29. 29

    why getline ignoring first character of input while using in loop?

HotTag

Archive