I am making a program that is converting from decimal to binary, octal, and hexadecimal. I am only focusing on the decimal to binary part in this so far. My problems are that the binary when I ask it to convert up to said number print them vertically not horizontally like 010. Also my while statement does not stop exacution if the y input is greater than 1024, which is the highest value I want to be able to be accepted.
import java.util.Scanner;
public class DNS
{
public static void main(String[] args)
{
int y;
Scanner input = new Scanner( System.in);
do
{
System.out.println("java DisplayNumberSystems");
System.out.println("Enter a decimal value to display to: ");
y = input.nextInt();
for(int x=0; x <=y; x++)
{
convertToBinary(x);
}
}
while(y <=1024);
}
public static void convertToBinary(int x)
{
if(x >0)
{
convertToBinary(x/2);
System.out.print(x%2 + " ");
}
System.out.println("");
}
}
remove
System.out.println("");frompublic static void convertToBinary(int x)you will be able to print horizontally
and change your do-while to simple while like this
you were checking if y<=1024 after calling convertToBinary() method. you have check if y<=1024 before making a call to convertToBinary() method.