I'm trying to write a program that takes in a positive integer as input, and output a string of 1’s and 0’s representing the integer in binary. Below is what i have coded so far, and im having trouble calling my metods in main. Please explaine what i am doing wrong. -------------------------------------------------------------------------------------------------------------------------------------------------------- import java.util.Scanner; public class binaryConversion { public static String integerToReverseBinary(int userInput){ String binaryString = ""; while(userInput != 0 ){ int binary = userInput % 2; binaryString = binary + binaryString; userInput = userInput / 2; } return binaryString; }//method public static String reverseString(String binaryString){ char [] letters = new char[binaryString.length()]; int letterIndex = 0; for (int i = binaryString.length() - 1; i >= 0; i-- ){ letters[letterIndex] = binaryString.charAt(i); letterIndex++; } String reverse = ""; for ( int i = 0; i < binaryString.length();i++){ reverse = reverse + letters[i]; } return reverse; }//method public static void main(String[] args) { Scanner scnr = new Scanner(System.in); //Variables int userInput; String rev; String string; //prompt the user to enter the integer that they would like to convert to binary System.out.println("Enter the number that will be converted: "); userInput = scnr.nextInt(); System.out.println(userInput+ " Converted to binary is: "); integerToReverseBinary(userInput); System.out.println("Number converted to binary: "+ binaryString); System.out.println(""); System.out.println(userInput + "Represented in reverse binary is: "); rev = reverseString(binaryString); System.out.println("The binary numbers in reverse are: " + reverse); scnr.close(); }//main }//Class
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
0’s representing the integer in binary. Below is what i have coded so far, and im having trouble calling my metods in main. Please explaine what i am doing wrong.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images