0
我想輸入並使用BufferedReader將它們添加到一個數組列表中。 代碼正在接受無盡的輸入,並且不會前進到更遠的行(不退出for循環)。 請找我下面的代碼:輸入爲arraylist使用readline()BufferedReader
public class ALinput {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n2 = br.read();// number of items in the list
ArrayList<String> list2 = new ArrayList<String>();
for(int i =1;i<=n2;++i)
{
list2.add(br.readLine());
}
System.out.println(list2);//printing list before sorting
Collections.sort(list2);//sorting the list
System.out.println("After sorting "+list2);
}
}
我已N2作爲數組列表元素的個數。如果輸入n2 = 5,則爲 ; readLine將5個字符串添加到數組列表後,不斷退出文本輸入而不退出。 它沒有出現for循環。請幫我理解我在這裏所犯的錯誤。
哦,我錯過了。是的,正確。謝謝。 – sagnikDas