Question: I'm using JavaScript Azul version. This is my code: import java.util.Scanner; //Main. Get input to which method to use** public class Main { public static
I'm using JavaScript Azul version. This is my code:
import java.util.Scanner; //Main. Get input to which method to use** public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x; // use Do-while loop to repeat menu. do { printMenu(); x = input.nextInt(); if (x == 1) { countPunctuation(); } else if (x == 2) { charactertoBinary(); } else if (x == 3) { binaryToCharacter(); //Inserting 4 will exit and end loop** } else if (x == 4) { System.out.println("You have chosen Exit. Goodbye"); // Any other input will give an error** } else { System.out.println("Error: Invalid input."); } //While loop will make it repeat** } while (x != 4); } // Create first Method. Print Menu*** public static void printMenu(){ String newline = System.getProperty("line.separator"); System.out.print("Enter '1' to count punctuation symbols in a String." + newline + "Enter '2' to convert characters to binary." + newline + "Enter '3' to convert 8-bit binary to a character." + newline + "Enter '4' to exit."+newline +"Enter option:"); } //countPunctuation Method** public static void countPunctuation(){ System.out.print("Enter a few sentences: "); Scanner input = new Scanner(System.in); // (Create input String(Set all punctuations to int 0)** String text=input.nextLine(); int periods=0; int questions=0; int commas=0; int exclaims=0; int apostrophes=0; int semicolons=0; int backslashes=0; // (for char a(Stores the character). Use toCharArray to convert to sequence of characters) for(char a : text.toCharArray()) { // (Take input and use ++ to add into the count)** if(a == '.')periods++; else if(a == '?')questions++; else if(a == ',')commas++; else if(a == '!')exclaims++; else if(a == '\'')apostrophes++; else if(a == ';')semicolons++; else if(a == '\\')backslashes++; } System.out.println("You entered " + periods + (periods == 1 ? " period," : " periods,") + " " + questions + (questions == 1 ? " question mark," : " question marks,") + " " + commas + (commas == 1 ? " comma," : " commas,") + " " + exclaims + (exclaims == 1 ? " exclamation mark," : " exclamation marks,") + " " + apostrophes + (apostrophes == 1 ? " apostrophe," : " apostrophes,") + " " + semicolons + (semicolons == 1 ? " semicolon," : " semicolons,") + " and " + backslashes + (backslashes == 1 ? " backslash." : " backslashes.")); } public static void charactertoBinary() { String newline = System.getProperty("line.separator"); Scanner input = new Scanner(System.in); System.out.println("Enter a few characters:"); //input n as integer. String binary as blank** int n = input.nextInt(); String binary = ""; do {// Use do while for math. Divide and take remainder of the int. int remainder = n % 2; n = n / 2; binary = remainder + binary; } while (n != 0); //Use.length to continue answer until it is 8 numbers long. while (binary.length() My question is on the method characterToBinary. I have the math right but my method only takes numbers and not symbols. I'm trying to figure out how to use char and to make it where I can convert a symbol to it's binary code. Also taking multiple symbols.
The example output is in this picture: 
Enter ' 1 ' to count punctuation symbols in a String. Enter ' 2 ' to convert characters to binary. Enter ' 3 ' to convert 8-bit binary to a character. Enter '4' to exit. 2 Enter a few characters: abcd abcd: 01100001011000100110001101100100 Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
