end of copying 2 Add an if statement that displays an err

//--------------- end of copying ---------------------

//#2 - Add an if statement that displays an error if the inputted Binary Number

Solution

Please follow the cod eand comments for description :

CODE :

import java.util.Scanner;

public class SnippetWeek07 { // class to run the code

public static void main(String[] args) { // driver method
int binaryDigitIndex = 7, //Used to cycle one index at a time through inputted Binary String in reverse order.
hexLoopCounter = 0, //Used to cycle through inputted Binary String in groups of 4.
decimalNumber = 0, //The Binary String converted into a decimal value
binaryDigitValue = 0, //Binary character digit converted to integer
powerFactorForPosition = 0, //Power of 2 factor for position to convert Binary To Decimal. 2 to the power of 0 through 2 to the power of 7.
hexSubstringIndex = 0; //
char binaryDigitChar = \' \'; //Single character extracted from inputted Binary String.
Scanner input = new Scanner(System.in); //Scanner object for obtaining input from the keyboard.
String binaryString = \"\", //The Binary String input by the user.
hexadecimalNumberString = \"\", //The Binary String converted to a hexidecimal String.
hexDigitString = \"\", //A 4 character Binary Substring converted to a single hexadecimal value as a String.
binarySubstring; //4 character group Substring of the inputted Binary String.

//*--- End of Declarations Section
System.out.println(\"Enter an 8 digit binary number and press Enter:\");
binaryString = input.nextLine();
System.out.println(\"\");

//Step #2 - if statement added here and make the follow part of the true block
//Loop to cycle through the inputted Binary String from right to left,
//obtain one character from the String, convert it a String hen to decimal,
//and convert the digit in the position. Add the decimal value to a
//running sum which will end up being the converted value for the entire
//Binary String.
if (binaryString.length() < 8 || binaryString.length() > 8) { // checking the condition to run the code
System.out.println(\"Entered String is Less/Greter than the Requirement.\"); // if do not satisfied reurning the message to the user
} else {
//Step #3 - While Loop statement using binaryDigitIndex cycling from 7 to 0
while (binaryDigitIndex != -1) {// iterating over the loop to run over the index values
binaryDigitChar = binaryString.charAt(binaryDigitIndex); // getting the character values at the defined index
System.out.println(\"Binary Digit in position \" + (binaryDigitIndex) + \" is \" + binaryDigitChar);
binaryDigitValue = Integer.parseInt(binaryDigitChar + \"\");
switch (binaryDigitIndex) { // switching over the index values over the factor positions
case 0:
powerFactorForPosition = (int) Math.pow(2, 7);
break;
//Step #4 - Add additional case clauses
case 1:
powerFactorForPosition = (int) Math.pow(2, 6);
break;
case 2:
powerFactorForPosition = (int) Math.pow(2, 5);
break;
case 3:
powerFactorForPosition = (int) Math.pow(2, 4);
break;
case 4:
powerFactorForPosition = (int) Math.pow(2, 3);
break;
case 5:
powerFactorForPosition = (int) Math.pow(2, 2);
break;
case 6:
powerFactorForPosition = (int) Math.pow(2, 1);
break;
case 7:
powerFactorForPosition = (int) Math.pow(2, 0);
break;
default:
System.out.println(\"Error! Invalid Binary Position!\");
powerFactorForPosition = 0;
}
decimalNumber += (binaryDigitValue * powerFactorForPosition);
binaryDigitIndex--; // decrementing the index value to get the data
}
System.out.println(\"\ The decimal equivalent of the # input is:\" + decimalNumber); // printing the decimal values of the input

//#5 for loop statement using hexLoopCounter here cycling from 0 to 4 stepping
//by an increment of 4.
for (hexLoopCounter = 0; hexLoopCounter <= 4; hexLoopCounter = hexLoopCounter + 4) { // looping over the counter values with the incrementing of 4
binarySubstring = binaryString.substring(hexLoopCounter, hexLoopCounter + 4);

switch (binarySubstring) { // switching over the substring values
case \"0000\":
hexDigitString = \"0\";
break;
//#6 - Add additional case clauses for \"0001\" (hex \"1\") to \"1111\" (hex \"F\").
case \"0001\":
hexDigitString = \"1\";
break;
case \"0010\":
hexDigitString = \"2\";
break;
case \"0011\":
hexDigitString = \"3\";
break;
case \"0100\":
hexDigitString = \"4\";
break;
case \"0101\":
hexDigitString = \"5\";
break;
case \"0110\":
hexDigitString = \"6\";
break;
case \"0111\":
hexDigitString = \"7\";
break;
case \"1000\":
hexDigitString = \"8\";
break;
case \"1001\":
hexDigitString = \"9\";
break;
case \"1010\":
hexDigitString = \"A\";
break;
case \"1011\":
hexDigitString = \"B\";
break;
case \"1100\":
hexDigitString = \"C\";
break;
case \"1101\":
hexDigitString = \"D\";
break;
case \"1110\":
hexDigitString = \"E\";
break;
case \"1111\":
hexDigitString = \"F\";
break;
default:
hexDigitString = \"ERROR! Invalid Binary String\";
}
hexadecimalNumberString += hexDigitString; //adding the result to the string
}
System.out.println(\"The Hexadecimal Equivalent is \" + hexadecimalNumberString); // printing the result
} //#2 else block
} //main() method.
}


OUTPUT :

Enter an 8 digit binary number and press Enter:
00010001

Binary Digit in position 7 is 1
Binary Digit in position 6 is 0
Binary Digit in position 5 is 0
Binary Digit in position 4 is 0
Binary Digit in position 3 is 1
Binary Digit in position 2 is 0
Binary Digit in position 1 is 0
Binary Digit in position 0 is 0

The decimal equivalent of the # input is:17
The Hexadecimal Equivalent is 11


Hope this is helpful.

//--------------- end of copying --------------------- //#2 - Add an if statement that displays an error if the inputted Binary NumberSolutionPlease follow the
//--------------- end of copying --------------------- //#2 - Add an if statement that displays an error if the inputted Binary NumberSolutionPlease follow the
//--------------- end of copying --------------------- //#2 - Add an if statement that displays an error if the inputted Binary NumberSolutionPlease follow the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site