Results 1 to 7 of 7
  1. #1
    sypherz is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Default 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);

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    null means, nothing... 0

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

    Try to print what
    Java 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,
    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

  3. #3
    sypherz is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Default thanks

    yes! thank you. i though that null == " " for some reason.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    sypherz is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Default

    String aString = null;//never creates sring object

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

    i guess thats the difference right?

  6. #6
    Join Date
    Jun 2008
    Posts
    5
    Rep Power
    0

    Default

    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()!="");

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2009, 02:47 AM
  2. Scanner Class Performance?
    By jazz2k8 in forum New To Java
    Replies: 1
    Last Post: 05-14-2008, 02:42 AM
  3. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:50 AM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •