我想讓用戶輸入一個字符串到數組中。什麼是最快的方法來檢查它們是否包含任何非字母字符?(例如#,&,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