# Decimal to Binary and octal

Can someone fix this???

import java.util.*;
public class Convert

{
public static void main(String[] args)

``````{
int num=0;
String result = "";
char repeat; //to hold yes or no
String OctStr; // to hold Decimal number
char c; //to hold each number of input octal
int i, j, n;

do
{
//for numbers 1 to 7, make a binary equivalent
//of that number to look up later
String biArray[]={"0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101","1111", "10000", "10001", "10010", "10011", "10100", "10101", "10110", "10111", "11000", "11001"};

Scanner input = new Scanner(System.in);
//System.out.println("Octal to Binary Conversion");
System.out.println("Enter the Decimal Number: ");
OctStr = input.nextLine();

//make a loop to read as many number as the input octal has
for (i=0; i<OctStr.length(); i++)
{
//read string by character then convert to an integer
c = OctStr.charAt(0);

for (j=0+1; j<c; j++)
{
//convert character to integer (ex: "1" to 1)don't know how???
n = Character.getNumericValue(c);
if(n <=25)
{
biArray[0] = "0";
biArray[1] = "1";
biArray[2] = "10";
biArray[3] = "11";
biArray[4] = "100";
biArray[5] = "101";
biArray[6] = "110";
biArray[7] = "111";
biArray[8] = "1000";
biArray[9] = "1001";
biArray[10] = "1010";
biArray[11] = "1011";
biArray[12] = "1100";
biArray[13] = "1101";
biArray[14] = "1110";
biArray[15] = "1111";
biArray[16] = "10000";
biArray[17] = "10001";
biArray[18] = "10010";
biArray[19] = "10011";
biArray[20] = "10100";
biArray[21] = "10101";
biArray[22] = "10110";
biArray[23] = "10111";
biArray[24] = "11000";
biArray[25] = "11001";

System.out.println(OctStr+ " convert to binary is "+ biArray[n]);
//System.out.println();
}//end if
else if(n>25)
System.out.println( "Not a valid Decimal number!");

}

}

// prompt for input more OctNum
System.out.print("Do you want to enter another Octal number? ");

System.out.println("Y for yes or N for No: ");
System.out.println();

String str= input.next(); //Read next char
repeat= str.charAt(0); //Get the first char

}while(repeat=='Y' || repeat=='y');

}//end main class
``````

}

//read string by character then convert to an integer
c = OctStr.charAt(0);
i think so that’s not exact effective code
try writing code with buffered reader and parseINTEGER

** DECIMAL TO BINARY
#include<stdio.h>
#include<conio.h>

dectobinary(int n)
{
int a;
if(n)
{
a=n%2;
dectobinary(n >> 1);
printf("%d",a);
}
}
void main()
{
int n;
clrscr();
printf(“Enter the number to convert”);
scanf("%d",&n);
a=dectobinary(n);
printf("%d",a);
getch();
}

Is there some reason why you cannot use Integer.parseInt( String s, int radix ) and Integer.toString( int n, int radix ) ?

//