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
*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``````
3
Contributors
5
Replies
12
Views
6 Years
Discussion Span
Last Post by bibiki

let's say 24546 is an octal number! what it actually says is that you have:

2*8^4 + 4*8^3 + 5*8^2 + 4*8^1 + 6*8^0, which then equals = 8192 + 2048 + 320 + 32 + 6, which then equals 10598 decimal. notice how the power of eight is being reduced by one, which explains "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?"

however, converting a decimal number into a binary number is different from converting an octal number into a binary number. which one do you want? do you want to convert an octal number directly to a binary number, or do you want to do it converting it to decimal and then to binary? what do you need to do?

Why have you started a new thread instead of continuing the old one?

Edited by NormR1: n/a

@NormR1
I have to start a new thread because when I replied, it always saying that my token was expired. Sorry for the confusing. Why do you need how do I want to see my output?

@bibiki (I like this name by the way)
either way would be fine for me: direct covert from octal to binary or through decimal. What ever way explain the result in a clean look would best, I guess.

Why do you need how do I want to see my output?

That's sort of a definition of what the program is to do.
If you show the input to the program and what the program is supposed to output, that helps define the code needed in the program.
Again:
why do you assign values to the biArray in two different places?

Why do you use two different Scanner methods: nextLine and next

The following line gets the first character of the input String. Is the String only 1 character long? You never get the rest of the characters.
c = OctStr.charAt(0);

Edited by NormR1: n/a

tracydo,
let's say you have octal number 75, which is 61 decimal. if you want to convert to binary, you break 75 to 7 and 5. you convert 7 to binary, which is 111, and 5 to binary, which is 101. you know put the 111 and 101 together, to get 111101, which is the binary equivalent of decimal 61, or octal 75. is it clear now? I think you have not understood the conversion process in the first place. confirm you understood it, and then we'll move on to helping you with writing the code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.