Commit 2ae5d42
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 0a369b9 commit 2ae5d42
File tree
1 file changed
+4
-8
lines changed- Section6StringClassPrinting/src
1 file changed
+4
-8
lines changedLines changed: 4 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 1 | + | |
| 2 | + | |
7 | 3 | | |
8 | 4 | | |
9 | | - | |
| 5 | + | |
10 | 6 | | |
11 | | - | |
| 7 | + | |
0 commit comments