A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Issue about ArrayList
Babu Singh
Ranch Hand
Joined: Aug 17, 2009
Posts: 68
posted
Aug 23, 2009 14:30:44
0
Hello,
please tell me why it doesnt enter in for loop when user stops inserting and just press enter i.e. null value.
import java.util.ArrayList; import java.util.Scanner; import java.util.Collections; public class ArraySorting { public static void main(String[] args) { //... Declare variables. Scanner in = new Scanner(System.in); ArrayList arraylist = new ArrayList ( ) ; //... Read input one word at a time. System.out.println("Enter words. End with EOF (CTRL-Z then Enter)"); System.out.println(" or CTRL-C."); //... Read input one word at a time, adding it to an array list. while (in.hasNext()) { arraylist.add(in.next()); } //... Sort the words. Collections.sort(arraylist); //To see the Elements of the arraylist System.out.println ( "The elements of arraylist are " ) ; for ( int i=0;i < arraylist.size ( ) ;i++ ) { System.out.println ( " "+ ( i+1 ) +" ) "+arraylist.get ( i ) ) ; } System.out.println ( "The size of arraylist ="+arraylist.size ( ) ) ; } }
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
posted
Aug 23, 2009 14:55:38
0
Works for me:
Enter words. End with EOF (CTRL-Z then Enter) or CTRL-C. Hello World The elements of arraylist are 1 ) Hello 2 ) World The size of arraylist =2
JDBCSupport
-
An easy to use, light-weight JDBC framework
-
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
Aug 23, 2009 16:06:03
0
Because it expects an end-of-file character as a signal to stop.
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8430
I like...
posted
Aug 23, 2009 21:46:04
0
Babu Ji wrote:
Please check your
private messages
for an important administrative matter
[
Donate a pint, save a life!
] [
How to ask questions
] [
Onff-turn it on!
]
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: Issue about ArrayList
Similar Threads
Writing to a file
new elements in the arraylist
Questions of how access methods of other class.
Count the number of vowels, words, and sentences.
Scanner methods
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter