2015-11-19 163 views

回答

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(); 

我希望它能帮助。

+1

什么是sc.close();做? – babyguineapig11

+1

它在使用后关闭扫描仪。所以如果你完成阅读你应该关闭你的扫描仪。 –

1

您可以使用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] = ""; 
    } 
} 
+0

使用大写'扫描仪':就像我的代码一样。 'Scanner sc = new Scanner(System.in);' – jiaweizhang

+0

在您尝试使用'array.length'之前,您的'array'是否被声明(并初始化)? – jiaweizhang

+0

是的,我已经初始化它。 – babyguineapig11

1

首先,您的问题非常模糊。像“我想让用户输入一个字符串到一个数组”的语句。我认为最有可能的意思是:“我想将用户的字符串输入放入数组中。”通常,应用程序的用户不会将字符串输入到数组中,这是执行代码的任务。您的所有用户应该做的是提供一个或多个字符串,这当然体现了问题....用户预期提供多少个字符串?好吧,我们假设它是无限的。该数组是否已经存在,并且它是否已经包含字符串元素?让我们假设,谁在乎。

我甚至不会问用户如何或在哪里预期输入这些所需的字符串。无论是来自控制台还是某种图形用户界面,在这一点上对我来说都不重要,因为它对你来说显然无关紧要。我们只是想完成工作,而且很酷。这是提供您已经尝试的代码对那些试图提供帮助的人有帮助的地方。你知道,帮助那些人来帮助你。

让我们从头开始,假设我们的数组是用来保存用户输入字符串还没有建立。我们将它命名为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; 
    } 

} 

我希望这有助于你一些。