How do I validate string within an array to a method in java

Patrick Cabrera :

How do I verify the input of the user to match the array in the call method, so that it will return the letter and show it along with validated it as a string?

String[] options1 = { "a", "b", "c" };  
choice = getValidString(sIn, "Please enter 'a', 'b' or 'c': ",
             "Invalid response. Only the letters 'a', 'b' or 'c' are acceptable.",
             options1); // call method
System.out.printf("The letter your entered was: %s\n\n", choice);

public static String getValidString(Scanner sIn, String question,
                                    String warning, String[] choices)
    String input = "";
    boolean valid= false;
    do {
        System.out.println(question);
        input = sIn.nextLine();
        try {
            Arrays.asList(choices).contains(input); // this is where the problem resides.
            valid = true;
        } catch(Exception e) {
        System.out.println(warning); } 
    } while (!valid);
    return input ;
}

Desired output:

Please enter 'a', 'b' or 'c': hypotenuse.
Invalid response. Only the letters 'a', 'b' or 'c' are acceptable.
Please enter 'a', 'b' or 'c': b
The letter your entered was: b
Arvind Kumar Avinash :

A Java array does not have any method like contains. Convert the array into a List using Arrays.asList and then you can apply contains on the resulting List.

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sIn = new Scanner(System.in);
        String[] options1 = { "a", "b", "c" };
        String choice = getValidString(sIn, "Please enter 'a', 'b' or 'c': ",
                "Invalid response. Only the letters 'a', 'b' or 'c' are acceptable.", options1); // call mehtod
        System.out.printf("The letter your entered was: %s\n\n", choice);
    }

    public static String getValidString(Scanner sIn, String question, String warning, String[] choices) {

        String input = "";
        boolean valid = false;
        List<String> choiceList = Arrays.asList(choices);
        do {
            System.out.println(question);
            input = sIn.nextLine();
            try {
                valid = choiceList.contains(input);
                valid = true;
            } catch (Exception e) {
                System.out.println(warning);
            }
        } while (!valid);
        return input;
    }
}

A sample run:

Please enter 'a', 'b' or 'c': 
b
The letter your entered was: b

Feel free to comment in case of any doubt/issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I append a string to an array of strings within a loop in C?

How do I validate character type and position within a string using a loop?

How do I add a string array within a string array array in a string list?

How do I pass a String Array inside of a method to another method?

How do i match a specific input within a string in Java?

How do I validate an array of integers in Laravel

How do I access an array within an array?

How do i access an array within an array

How do I parse Array within Array

How do I print an array as a String from a method?

How do I call a method within a setString

How do I check if a string exists within a string within a column

How do I pick a string from a larger string within an array and group them with name?

HashMap String, String Array - How do I get a certain index within a key?

Java - How do I call a class's method with a string?

How do I create a method that capitalizes the first letter of a string in java?

How do I reverse a string within a for loop?

How do I add +string within a text?

How do I access a property within an array from a method in Vue js?

How do I link within an object/array?

In Java, how do I most efficiently find an array within a JSON array based on the value of an element of the subarray?

How do I get the method name from within that method?

How do I validate an enum parameter on a public method?

How do I update an array within an object from within state?

How do I get a value within a class stored within an array?

Java - How to validate this string?

How do I list all local variables within a Java method / function?

How do I run a java method against all files of a type within a gradle project?

Java - How do I get a variable generated from within a method in one class to another class?