我想让用户输入一个字符串到数组中。什么是最快的方法来检查它们是否包含任何非字母字符?(例如#,&,8,[,0) 非常感谢!如何在数组字符串中搜索非字母字符?
回答
你可以这样来做:
int size = 1024;
String[] strs = new String[size];
Scanner sc = new Scanner(System.in);
for (int i=0; i<size; i++) {
String input = sc.next();
if (input.matches("[A-Za-z]")) {
strs[i] = input;
} else {
strs[i] = "";
}
}
sc.close();
我希望它能帮助。
什么是sc.close();做? – babyguineapig11
它在使用后关闭扫描仪。所以如果你完成阅读你应该关闭你的扫描仪。 –
您可以使用Scanner
采取用户输入:
String alphabet = "abcdefghijklmnopqrstuvxyz";
int arraySize = 10;
String[] charArray = new String[arraySize];
Scanner sc = new Scanner(System.in);
for (int i=0; i<arraySize; i++) {
String input = sc.next();
if (!alphabet.contains(input) {
charArray[i] = input;
} else {
charArray[i] = "";
}
}
使用大写'扫描仪':就像我的代码一样。 'Scanner sc = new Scanner(System.in);' – jiaweizhang
在您尝试使用'array.length'之前,您的'array'是否被声明(并初始化)? – jiaweizhang
是的,我已经初始化它。 – babyguineapig11
首先,您的问题非常模糊。像“我想让用户输入一个字符串到一个数组”的语句。我认为最有可能的意思是:“我想将用户的字符串输入放入数组中。”通常,应用程序的用户不会将字符串输入到数组中,这是执行代码的任务。您的所有用户应该做的是提供一个或多个字符串,这当然体现了问题....用户预期提供多少个字符串?好吧,我们假设它是无限的。该数组是否已经存在,并且它是否已经包含字符串元素?让我们假设,谁在乎。
我甚至不会问用户如何或在哪里预期输入这些所需的字符串。无论是来自控制台还是某种图形用户界面,在这一点上对我来说都不重要,因为它对你来说显然无关紧要。我们只是想完成工作,而且很酷。这是提供您已经尝试的代码对那些试图提供帮助的人有帮助的地方。你知道,帮助那些人来帮助你。
让我们从头开始,假设我们的数组是用来保存用户输入字符串还没有建立。我们将它命名为inputStringArray,并且我们有一个变量用于保存来自User的字符串输入,我们将其命名为inputString。以下代码过度评论的代码应该照顾业务。
创建名为UserInputToArray一个新的Java应用程序项目,然后复制/粘贴在自动创建类下面的代码(在NetBeans反正):
public class UserInputToArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Where you declare or establish your input strings
// array is up to you as long as the scope of the
// varaible can reach our call to the addUserInputToArray
// method below.
String[] inputStringArray = {};
// How you acquire the User's input string is up to you...
String inputString = "Hello There";
// Pass our input string array and user string input to
// our addUserInputToArray() method and let it modify
// or rather append the User input string to our inputStringArray
// array variable.
inputStringArray = addUserInputToArray(inputStringArray, inputString);
// This is used just to test our input string array so that
// you can see that the User input string has been successfully
// appended to the inputStringArray array. you can omit it.
for (int i = 0; i < inputStringArray.length; i++){
System.out.println(inputStringArray[i]);
}
}
/**
* This method is used to append a User Input String to our
* inputStringArray[] variable.
* @param stringArray : (String Array) This is where you supply the
* input string array variable.
*
* @param inputString : The User's supplied input string is provided here.
*
* @return : A String array with the Users string input appended to it but
* only if it is found that the string only contains Alphabetic characters
* (a to z and A to Z and spaces).
*/
public static String[] addUserInputToArray(String[] stringArray, String inputString) {
// Get the length of our input string array and add 1.
//This is used so we don't have to type stringArray.length
//all the time.
int length = (stringArray.length + 1);
// Here we use the string matches method with a small regex
// expression string to make sure only alphabetic charaters are
// contained within the supplied User input string. Expression
// breakdown:
// (?i) Ignore letter case (we don't need to worry about that in this case).
// [ a-z] Match any characters that are either a to z or a space.
// * Any number of characters (strings can be any length).
// () All enclosed in a set of brackets to create a group. Not
// really required in this case (just a habbit).
if (inputString.matches("((?i)[ a-z]*)")) {
// So, our acquired User input string has passed requirements and
// now it's time to append that string to our input string array.
// As you know there is no such thing as appending to an array so
// we need to simulate it and to do that we need to create a temporary
// array, increase its length to what is desired which in our case is
// once (1) every time this method is called, and then copy our passed
// original input string array into it while preserving the length of
// our temporary array and then finally forcing our original input
// string array to be our temporary array. Now we have a input string
// array which is one element size bigger than when we started and ready
// to have string data placed into it.
String[] tmp = new String[length];
if (stringArray.length != 0) {
System.arraycopy(stringArray, 0, tmp, 0, Math.min(stringArray.length, tmp.length));
}
stringArray = tmp;
// Append our User input string to the array.
stringArray[length-1] = inputString;
}
return stringArray;
}
}
我希望这有助于你一些。
- 1. 查询在非字母数字列中搜索字母数字字符串
- 2. 搜索字母数字字符串
- 3. 字符数组搜索子字符串
- 4. 在循环中搜索一个非字母字符的字符串
- 5. 字符串搜索数组
- 6. 搜索字符数组串
- 7. 如何为特定字符串搜索字符串数组
- 8. 如何搜索字符串/字符数组中的模式?
- 9. 在字符串数组中搜索精确的字符串?
- 10. Java - 在字符串数组中搜索字符串
- 11. 在字符串数组项目元素中搜索字符串
- 12. 在字符串中搜索字符串并制作数组
- 13. 在字符串数组中搜索子字符串?
- 14. 如何搜索字符串数组
- 15. 在字符串中搜索字母组合php
- 16. 字符串与非字母
- 17. 在Java中搜索字符串数组?
- 18. 在mysql中搜索字符串数组
- 19. 在数组中搜索字符串
- 20. Hibernate在字符串数组中搜索
- 21. 数字搜索字符串
- 22. Javascript字符串搜索,双字母出现在字母i
- 23. 如何在字母数字字符串
- 24. 如何让mod_rewrite在查询字符串中放置非字母数字字符?
- 25. 从字符串中删除非字母数字字符
- 26. 替换字符串中的所有非字母数字字符
- 27. 如何清除字符串中连续的非字母字符?
- 28. 如何从字符串中删除非字母字符?
- 29. Java Lucene:搜索包含非字母数字字符的术语
- 30. 搜索字符串中的字符串
欢迎来到SO!最快的意思是什么,为什么这很重要? – Kenney