我想通過用戶System.in將我的單詞添加到我的input_words中,我嘗試過在線查看,並且測試了許多方法,但無法正常工作。任何人都可以幫忙如何使用BufferedReader將字符串添加到ArrayList中?
在我的代碼中,當我運行它時,它會要求我放入String1,2,3 ... 7,但我想將它放入input_words。
謝謝...
這是我的代碼。
public static void main(String[] args) {
ArrayList<String> input_words = new ArrayList<String>();
input_words.add("SCHOOLWORKTWO");
input_words.add("BALCONY");
input_words.add("INSIST");
input_words.add("SALTPETER");
input_words.add("BOLTON");
input_words.add("KITSCHY");
input_words.add("CLIENTELE");
String s1 = getInput("Enter a String String 1: ");
String s2 = getInput("Enter a String String 2: ");
String s3 = getInput("Enter a String String 3: ");
String s4 = getInput("Enter a String String 4: ");
String s5 = getInput("Enter a String String 5: ");
String s6 = getInput("Enter a String String 6: ");
String s7 = getInput("Enter a String String 7: ");
System.out.println(s1 + ", " + s2+", " + s3+", " + s4+", " + s5+", " + s6+", " + s7); // Should use this string as input_words.add Array
System.out.print(getTiles(input_words, DEFAULT_SPLITTER)); // Make sure to use DEFAULT_SPLITTER
}
這一個也會有幫助,但我的問題是關於轉換我的命令行,它是輸入字符串1,輸入字符串2 ....輸入字符串7,然後我想要所有這些字符串到我的input_words Arraylist。我試圖找出很多方法,但我不知道該怎麼做。 – user1997751
如果您希望程序要求用戶輸入字符串1,字符串2等,您可以簡單地在input_words.add(input。)上方放置一個「System.out.println(」Enter string「+ i)」語句。 next())(在循環內)。 – ObedMarsh