Results 1 to 2 of 2
  1. #1
    Magnesium is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Question How do you scan a string with spaces?

    How do you input a string that has spaces in it?

    For example in this program

    Java Code:
    System.out.print("Type something: "); 
    		
    Scanner scan = new Scanner(System.in); 
    		
    String x = scan.next(); 
    		
    System.out.println(x);
    if you type in, say, 'hello world', it will only print 'hello'. Why is this? Is there a way to get input for a string with spaces?

    Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How do you scan a string with spaces?

    Have you looked at the API for Scanner? There's a method that
    Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Removing spaces in String ArrayList
    By dougie1809 in forum New To Java
    Replies: 6
    Last Post: 03-13-2012, 09:25 PM
  2. how to use Scanner to scan a String
    By RichersooN in forum New To Java
    Replies: 1
    Last Post: 09-23-2011, 05:14 AM
  3. Replies: 1
    Last Post: 09-15-2011, 01:51 PM
  4. How to display a String that contains more than one white spaces
    By thachun in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-27-2009, 09:16 AM
  5. Replies: 3
    Last Post: 08-20-2009, 03:57 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
  •