Scanner input question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandyw
    New Member
    • Mar 2007
    • 122

    Scanner input question

    have an scanner which prompts the user to
    type some data into it.
    for example
    Lets say I have four strings.
    String id
    string name // this includes first and last name
    string address //this includes the full address.
    string price...

    When the first scanner prompts the user they type in
    DW123 and hits return
    the next scanner prompts the user to type i the name
    James Brown. and hits return.
    when this happens I will get the next scanner running together
    example
    enter your address==>,ente r the price==>

    yet if I were to enter the name as JamesBrown it will work.

    is there away around this.
    any help would be greats

    Sandy
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by sandyw
    have an scanner which prompts the user to
    type some data into it.
    for example
    Lets say I have four strings.
    String id
    string name // this includes first and last name
    string address //this includes the full address.
    string price...

    When the first scanner prompts the user they type in
    DW123 and hits return
    the next scanner prompts the user to type i the name
    James Brown. and hits return.
    when this happens I will get the next scanner running together
    example
    enter your address==>,ente r the price==>

    yet if I were to enter the name as JamesBrown it will work.

    is there away around this.
    any help would be greats

    Sandy
    When you do a Scanner.next() for reading a name and type "James Brown",
    the Scanner reads a String which is "James" and stops reading. For the
    address the Scanner will read "Brown" (it hasn't been read yet) and the
    Scanner continues reading the price.

    Better use Scanner.nextLin e() for the name and full address if they contain
    spaces. Correctly scanning human generated input is always difficult.

    kind regards,

    Jos

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Originally posted by JosAH
      When you do a Scanner.next() for reading a name and type "James Brown",
      the Scanner reads a String which is "James" and stops reading. For the
      address the Scanner will read "Brown" (it hasn't been read yet) and the
      Scanner continues reading the price.

      Better use Scanner.nextLin e() for the name and full address if they contain
      spaces. Correctly scanning human generated input is always difficult.

      kind regards,

      Jos
      Hey I can use this in the next article.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by nomad
        Hey I can use this in the next article.
        Everybody learns everyday ;-)

        kind regards,

        Jos

        Comment

        Working...