如何在Java中将数组中的字符串验证到一个方法中

huangapple go评论77阅读模式
英文:

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

问题

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 {
            if (Arrays.asList(choices).contains(input)) {
                valid = true;
            } else {
                System.out.println(warning);
            }
        } catch(Exception e) {
            System.out.println(warning);
        } 
    } while (!valid);
    return input;
}
英文:

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

答案1

得分: 0

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); // 调用方法
        System.out.printf("The letter you 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;
    }
}

一个样例运行:

Please enter 'a', 'b' or 'c':
b
The letter you entered was: b
英文:

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 = { &quot;a&quot;, &quot;b&quot;, &quot;c&quot; };
		String choice = getValidString(sIn, &quot;Please enter &#39;a&#39;, &#39;b&#39; or &#39;c&#39;: &quot;,
				&quot;Invalid response. Only the letters &#39;a&#39;, &#39;b&#39; or &#39;c&#39; are acceptable.&quot;, options1); // call mehtod
		System.out.printf(&quot;The letter your entered was: %s\n\n&quot;, choice);
	}

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

		String input = &quot;&quot;;
		boolean valid = false;
		List&lt;String&gt; 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 &#39;a&#39;, &#39;b&#39; or &#39;c&#39;: 
b
The letter your entered was: b

huangapple
  • 本文由 发表于 2020年4月6日 23:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61062925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定