• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use regular expressions for Integer Input  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have a program in which I take some characters input from User

using
String inputString=in.next();

How can i restrict user that input should be only char 0 or 1?

if the answar is regular expressions then What Should be Its Regular expressions?


Thanks
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you want a binary number only?

I would prefer not to use a regex if possible. They are awkward to write and there can be subtle errors you might miss. You can pass text to a Scanner as in this thread and it will tell you whether it is an int. You can probably find an overloaded version of that method which takes a radix: for binary you would use radix = 2.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation for Scanner has a grammar for numbers; you might find that useful (or you might find it incomprehensible ‍).
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume Campbell means Scanner .nextInt(int radix)
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
privillage denied to access that thread you link with your post
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the privileges so clicking the link in my old post (repeated here) should now work.

And sorry for the mistake.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:
How can i restrict user that input should be only char 0 or 1?

if the answar is regular expressions then What Should be Its Regular expressions?


Checking that a string only contain zeros and ones is pretty straightforward for regular expressions -- and is likely one of the elementary examples when learning regular expressions. What have you tried so far? And what are you having an issue with?

Henry
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading your(campbell) and other Guys post there.

I have one question is there any way I can verify userInput should only contain binary number, other than using "regex"?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I have corrected my privilege mistake, did you read the thread I linked to?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:After reading your(campbell) and other Guys post there.

I have one question is there any way I can verify userInput should only contain binary number, other than using "regex"?

There are always other ways to do things. I can think of several ways...For example:

Count how many '1's are in the string. Count how many '0's are in the string. Then see if those two values add up to the total length of the string.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!