65.3k views
1 vote
The following if statement is incomplete, but it should display the word "digit" if the char variable ch contains a numeric digit. Otherwise, the statement should display "not a digit." What boolean expression would you write inside the parentheses of the if statement? if (__________) System.out.println("digit"); else System.out.println("not a digit");

1 Answer

1 vote

Final answer:

To display 'digit' if the char variable ch is a numeric digit, use Character.isDigit(ch) in the if statement. If ch is not a digit, 'not a digit' will be displayed.

Step-by-step explanation:

To complete the if statement to display the word "digit" if the char variable ch contains a numeric digit, you would need to check if ch is between '0' and '9'. You can write a boolean expression that uses Character.isDigit(ch) method, which returns true if the specified character is a digit. Alternatively, you can use a range check like (ch >= '0' && ch <= '9'). Here's the complete if statement using the Character.isDigit method:

if (Character.isDigit(ch))
System.out.println("digit");
else
System.out.println("not a digit");

Both ways will achieve the intended result, but using Character.isDigit makes the code easier to read and understand.

User Shakeeb Ahmed
by
8.4k points