Skip to content

Commit 2ae5d42

Browse files
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

1 file changed

+4
-8
lines changed
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
//Find number binary or not.
2-
3-
public class RegularExpressionChallenge
4-
{
5-
public static void main(String[] args)
6-
{
1+
public class RegularExpressionChallenge {
2+
public static void main(String[] args) {
73
int b = 1000110;
84
String str = String.valueOf(b);
9-
System.out.println(str.matches( "[01]*"));
5+
System.out.println(str.matches("[01]*"));
106
}
11-
}
7+
}

0 commit comments

Comments
 (0)