英文:
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 = { "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论