Check that a String entered contains only 1's and 0's

前端 未结 2 1783
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 08:14

I am writing a program that switches a binary number into a decimal number and am only a novice programmer. I am stuck trying to determine that the string that was entered is a

相关标签:
2条回答
  • 2021-02-14 08:54

    You could just use a regular expression:

    num.matches("[01]+")
    

    This will be true if the string num contains only 0 and 1 characters, and false otherwise.

    0 讨论(0)
  • 2021-02-14 09:11

    n!=1 || n!=0 is always true because you cannot have n = 0 and 1 at the same moment. I guess you wanted to write n!=1 && n!=0.

    0 讨论(0)
提交回复
热议问题