Results 1 to 4 of 4
Thread: scanner to read string or int
- 05-03-2010, 01:53 PM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
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
- 05-03-2010, 02:10 PM #2
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
- 05-03-2010, 10:17 PM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
thanks. will read up on how to parse
joe
- 05-04-2010, 12:09 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
Using Read/Write From File Using Scanner
By javaisntcoffee123 in forum New To JavaReplies: 4Last Post: 04-15-2010, 03:35 AM -
How to make a scanner read an int?
By Dieter in forum New To JavaReplies: 3Last Post: 11-08-2009, 01:11 AM -
Using Scanner class to read int value
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2009, 02:47 AM -
Using Scanner class to read int
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:50 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks