Commit a4788c1
committed
feat: Add regex-based check to determine if a number is binary
- Converts an integer value (e.g., 1000110) into a String using `String.valueOf()`.
- Uses `String.matches("[01]*")` to validate whether the number contains only binary digits (0s and 1s).
- The regular expression `[01]*` ensures the string contains **zero or more** characters that are either '0' or '1'.
- Prints `true` if the input is a valid binary number; otherwise, prints `false`.
Example:
Input: 1000110
Output: true (since all digits are either 0 or 1)
This implementation leverages Java’s regex API for a concise binary number validation approach.
Signed-off-by: Somesh diwan <[email protected]>1 parent 2ae5d42 commit a4788c1
File tree
1 file changed
+2
-0
lines changed- Section6StringClassPrinting/src
1 file changed
+2
-0
lines changedLines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
0 commit comments