Results 1 to 4 of 4
  1. #1
    joecast is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default scanner to read string or int

    hello all,
    just getting into java and have a question i am sure has been asked many times. i could not find anything through the search so here is my question.

    i am asking the user for some input which may be some text or an integer. how can i analyse both the string and int without knowing which will be entered?

    if the user enters 1 - 9 > some action will occur, else if user enters "off" > some other action will occur.

    if i add lines for ### = sc.nextInt() and ### = sc.next() after my S.O.P will that be enough? Thanks in advance.
    joe

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    use sc.nextLine(), if it's "off" you turn off, if not you try to parse the int. If that doesn't work you have an illegal input.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    joecast is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks. will read up on how to parse
    joe

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

Similar Threads

  1. Using Read/Write From File Using Scanner
    By javaisntcoffee123 in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 04:35 AM
  2. How to make a scanner read an int?
    By Dieter in forum New To Java
    Replies: 3
    Last Post: 11-08-2009, 02:11 AM
  3. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:50 PM
  4. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:34 AM
  5. Replies: 0
    Last Post: 11-20-2007, 05:59 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
  •