2015-11-19 161 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; 
    } 

} 

我希望這有助於你一些。