Guidance ======================= Getting the input ----------------- Use the Sca
ID: 3841679 • Letter: G
Question
Guidance
=======================
Getting the input
-----------------
Use the Scanner class nextLine method to input the binary number as a String.
Use the String charAt() method in a "for" loop that varies the index of the
character being examined. If your loop control variable is "i", then you will
use charAt(i) in the loop body to be able to examine the character at position "i".
As the loop progresses, you are able to get each character one-by-one. The
characters in a String are indexed from 0 to 1 less than the number of characters
in the String. The number of characters in the String can be determined by using
the String length() method.
Suppose you named the input String sInput. Then your "for" loop header would look like:
for (int i = 0; i <= sInput.length() - 1; i++)
Note that charAt() returns a character as type "char". For comparison, remember that
character literals are enclosed in single quotation marks, like this: '1'.
Error message
-------------
When an invalid string is detected, display an error message, and then use a "return"
statement to exit from the main method (ending the program).
Purpose of "for" loop
---------------------
You must think about and understand the purpose of your "for" loop.
Don't try to fit your entire program into it.
For example, the "for" loop could count the number of invalid characters
(anything other than a '0' or a '1') and also it could count the number of
'1's it found.
Then, after the "for" loop is finished, that is after its closing brace, you
could have other code with "if" and/or "else" statements to decide what to output.
Of course, that means any variables you use in your post-"for" loop code must
be declared prior to the start of the "for" loop so they will still be in scope
after it finishes.
Output ===================== The following 3 test runs illustrate the program'se input and output: Enter a binary number > abc Invalid binary number. Enter a binary number > 01110000001 Rejected Enter a binary number > 1000001 Accepted Note: Each of these requires a separate run (execution) of the program.
Guidance
=======================
Getting the input
-----------------
Use the Scanner class nextLine method to input the binary number as a String.
Use the String charAt() method in a "for" loop that varies the index of the
character being examined. If your loop control variable is "i", then you will
use charAt(i) in the loop body to be able to examine the character at position "i".
As the loop progresses, you are able to get each character one-by-one. The
characters in a String are indexed from 0 to 1 less than the number of characters
in the String. The number of characters in the String can be determined by using
the String length() method.
Suppose you named the input String sInput. Then your "for" loop header would look like:
for (int i = 0; i <= sInput.length() - 1; i++)
Note that charAt() returns a character as type "char". For comparison, remember that
character literals are enclosed in single quotation marks, like this: '1'.
Error message
-------------
When an invalid string is detected, display an error message, and then use a "return"
statement to exit from the main method (ending the program).
Purpose of "for" loop
---------------------
You must think about and understand the purpose of your "for" loop.
Don't try to fit your entire program into it.
For example, the "for" loop could count the number of invalid characters
(anything other than a '0' or a '1') and also it could count the number of
'1's it found.
Then, after the "for" loop is finished, that is after its closing brace, you
could have other code with "if" and/or "else" statements to decide what to output.
Of course, that means any variables you use in your post-"for" loop code must
be declared prior to the start of the "for" loop so they will still be in scope
after it finishes.
Output ===================== The following 3 test runs illustrate the program'se input and output: Enter a binary number > abc Invalid binary number. Enter a binary number > 01110000001 Rejected Enter a binary number > 1000001 Accepted Note: Each of these requires a separate run (execution) of the program.
Output ===================== The following 3 test runs illustrate the program'se input and output: Enter a binary number > abc Invalid binary number. Enter a binary number > 01110000001 Rejected Enter a binary number > 1000001 Accepted Note: Each of these requires a separate run (execution) of the program.
Explanation / Answer
import java.util.Scanner;
public class ValidateBinary {
/**
* @param args
*/
public static void main(String[] args) {
// declaration
int invalidChars = 0, validChars = 0;
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
// prompt to read bynary string
System.out.print("Enter a binary number > ");
String binaryNum = scanner.nextLine();
// repeat the loop upto length
for (int i = 0; i < binaryNum.length(); i++) {
if (binaryNum.charAt(i) == '1')
validChars++;
else if (binaryNum.charAt(i) == '0')
;
else
invalidChars++;
}
// check the condition for valid
if (invalidChars > 0)
System.out.println("Invalid binary number.");
else if (validChars == 2)
System.out.println("Accepted");
else
System.out.println("Rejected");
} catch (Exception e) {
// TODO: handle exception
} finally {
if (scanner != null)
scanner.close();
}
}
}
OUTPUT:
Enter a binary number > abc
Invalid binary number.
Enter a binary number > 01110000001
Rejected
Enter a binary number > 1001
Accepted
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.