Commit 0a369b9
committed
feat: Implement binary to decimal conversion using Scanner.useRadix(2)
- Prompt user to input a binary number (comprising only 0s and 1s).
- Configured Scanner to interpret input using base-2 (`useRadix(2)`).
- Used `nextInt()` to parse the binary string into a decimal integer.
- Wrapped parsing logic in a try-catch block to handle InputMismatchException.
- Displays an appropriate error message if non-binary input is entered.
- Used a `finally` block to ensure Scanner resource is properly closed.
- Included a comment with an example to explain the binary-to-decimal calculation.
Example:
Input: 1010
Output: Decimal value: 10
This commit demonstrates robust input handling and introduces a simple yet effective use of Java's radix-based parsing for binary number systems.
Signed-off-by: Somesh diwan <[email protected]>1 parent 84ed62e commit 0a369b9
1 file changed
+20
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
0 commit comments