@NormR1 or anyone please help,
This program is to convert the input octal to output the binary in multiple number within the octal (ex: 24546, not 4 or 2 in single input)using an array. Thanks
I converted it to integer, but how do I read one integer at a time in the input octal number until it complete?
NormR1 has mentioned that each digit character has a value 10 times the next one (for a decimal number). For an octal number, the value of each digit is 8 times. How do I do that?
In my other post showed the result of very long, and only pick up the first number.
/*
*File:OctalBinary1.java
*Write a user friendly interactive program to allow a user to enter an
*octal digit(0-7)and output the binary equivalent (or an error message
*if not in range) for as many #'s asfor as many #'s as the user wishes.
*Use the direct access method. You may initialize the array in the
*declaration instead of reading it in.
*Inputs: From keyboard ? octal #'s to be translated (converted)
*Outputs: Interaction with user and conversion of # or error message
*/
import java.util.*;
public class OctalBinary1
{
public static void main(String[] args)
{
int num=0;
String result = "";
char repeat; //to hold yes or no
String OctStr; // to hold Octal 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[]={"000", "001", "010", "011", "100", "101", "110", "111"};
Scanner input = new Scanner(System.in);
System.out.println("Octal to Binary Conversion");
System.out.println("Enter the Octal 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 <8)
{
biArray[0] = "000";
biArray[1] = "001";
biArray[2] = "010";
biArray[3] = "011";
biArray[4] = "100";
biArray[5] = "101";
biArray[6] = "110";
biArray[7] = "111";
System.out.println(OctStr+ " convert to binary is "+ biArray[n]);
System.out.println();
}//end if
else if(n>7)
System.out.println( "Not a valid octal 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
}//end