我希望用户将字符串输入到数组中。执行此操作的最快方法是检查他们估算的字符串是否包含任何非字母字符?(例如#,&,8,[,0) 非常感谢!
答案 0 :(得分:1)
您可以使用public static boolean isPalindrome(int number) {
String numString = number + "";
for(int i = 0; i < numString.length() / 2; i++){
if(numString.charAt(i) != numString.charAt(numString.length() - i){
return false;
}
}
return true;
}
接收用户输入:
Scanner
答案 1 :(得分:1)
你可以这样做:
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();
我希望它有所帮助。
答案 2 :(得分:1)
首先,你的问题很模糊。一句话,“我希望用户将字符串输入到数组中”。我认为最有可能的意思是:“我希望将用户的字符串输入放入数组中。”通常,应用程序的用户不会将字符串输入到数组中,这是代码执行的任务。您的所有用户都应该提供一个或多个字符串,这当然会显示问题....用户期望提供多少字符串?好的,让我们假设它是无限的。数组是否已存在并且是否已包含字符串元素?让我们假设,谁在乎。
我甚至不想询问用户输入这些所需字符串的方式和位置。无论是来自控制台还是某种GUI,对我来说真的无关紧要,因为它显然对你无关紧要。我们只想完成工作而且很酷。这就是提供您已经尝试过的代码对那些试图提供帮助的人有用的地方。你知道,帮助那些人帮助你。
让我们重新开始并假设尚未建立用于保存用户输入字符串的数组。让我们将其命名为inputStringArray,我们有一个变量,用于保存用户输入的字符串,我们将其命名为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;
}
}
我希望这会对你有所帮助。