Results 1 to 7 of 7
Thread: Scanner class problems
- 06-06-2008, 02:30 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 3
- Rep Power
- 0
Scanner class problems
hi, i want to read in entire lines of from the user
and have the loop terminate when user hits enter on a blank line
the loop never ends! please help. my this is just a test class for
the rest of a program i am just abo0ut finished
this is what i have for the loop:
Java Code:String aString; System.out.println("Enter sentences, after each sentence hit enter\n" + "hit enter on an empty line to stop :\n"); do { System.out.println("beginning of iteration\n"); aString = sc.nextLine(); stringData.add(aString); System.out.println("end of iteration\n"); }while(sc.nextLine() != null);
- 06-06-2008, 03:19 AM #2
null means, nothing... 0
How about enter? that generates a newline right?values 0A in hex....
Try to print whatprints, since that comparison always true, then sc.nextLine() should have captured some text from console...Java Code:while(sc.nextLine() != null);
and i guess, that will prints "end of iteration"
try like,Java Code:while(sc.nextLine().equals("");
or another flag that could negate the comparison,
eg. if(sc.nextLine().equals("x") then terminate....freedom exists in the world of ideas
- 06-06-2008, 05:22 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 3
- Rep Power
- 0
thanks
yes! thank you. i though that null == " " for some reason.
- 06-06-2008, 05:56 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You got a collision with null and empty string there.
- 06-06-2008, 04:58 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 3
- Rep Power
- 0
String aString = null;//never creates sring object
String aString = " ";//memory IS allocated for string object.
i guess thats the difference right?
- 06-07-2008, 04:13 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 5
- Rep Power
- 0
Yeah, the null is just a null pointer, i.e. a pointer which doesn't point to any object in memory. With the second one the string " " is stored in memory and the String object aString contains a pointer to that string.
When you test the user input first use the trim() method on the string. That is:
Java Code:while(sc.nextLine().trim()!="");
- 06-09-2008, 09:46 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can still test null too here. It's better too.
Similar Threads
-
Using Scanner class to read int value
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2009, 02:47 AM -
Scanner Class Performance?
By jazz2k8 in forum New To JavaReplies: 1Last Post: 05-14-2008, 02:42 AM -
Using Scanner class to read int
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:50 AM -
Scanner class
By ajaymenon.k in forum Advanced JavaReplies: 1Last Post: 11-26-2007, 07:01 AM -
JDK 5.0 Scanner Class
By Sircedric88 in forum New To JavaReplies: 3Last Post: 07-27-2007, 06:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks