Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-06-2008, 04:30 AM
Member
 
Join Date: Jun 2008
Posts: 3
sypherz is on a distinguished road
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:
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);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-06-2008, 05:19 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
null means, nothing... 0

How about enter? that generates a newline right?values 0A in hex....

Try to print what
Code:
while(sc.nextLine() != null);
prints, since that comparison always true, then sc.nextLine() should have captured some text from console...

and i guess, that will prints "end of iteration"

try like,
Code:
while(sc.nextLine().equals("");

or another flag that could negate the comparison,

eg. if(sc.nextLine().equals("x") then terminate....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-06-2008, 07:22 AM
Member
 
Join Date: Jun 2008
Posts: 3
sypherz is on a distinguished road
thanks
yes! thank you. i though that null == " " for some reason.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-06-2008, 07:56 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You got a collision with null and empty string there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-06-2008, 06:58 PM
Member
 
Join Date: Jun 2008
Posts: 3
sypherz is on a distinguished road
String aString = null;//never creates sring object

String aString = " ";//memory IS allocated for string object.

i guess thats the difference right?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-07-2008, 06:13 PM
Member
 
Join Date: Jun 2008
Posts: 5
pjm35@st-and.ac.uk is on a distinguished road
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:

Code:
while(sc.nextLine().trim()!="");
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-09-2008, 11:46 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You can still test null too here. It's better too.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scanner Class Performance? jazz2k8 New To Java 1 05-14-2008 04:42 AM
Using Scanner class to read int Java Tip Java Tips 0 01-18-2008 01:50 PM
Using Scanner class to read int value Java Tip Java Tips 0 12-03-2007 11:34 AM
Scanner class ajaymenon.k Advanced Java 1 11-26-2007 09:01 AM
JDK 5.0 Scanner Class Sircedric88 New To Java 3 07-27-2007 08:55 PM


All times are GMT +3. The time now is 02:17 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org