Hi,
So I just done this class for the semester but am still having this problem in the back of my head that I need your help to clear it out of the way.

Basically, the program here about covert the Octal number to Binary number which Octal number will be input by user from DOS window. I can only convert 1 single Octal number at a time and prompt "invalid" every time it's larger than 7.

How do I convert more than 1 (ex: 34526 or even more)?
My professor said something like I first have to isolate each octal number, then read it as string or char to each equivalent binary number (group of 3: 000, 001, ..., 111). Thanks

/*
*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
		int OctNum;
		
		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: ");
				OctNum = input.nextInt();
				
				if(OctNum <=7)	
				{	
					System.out.println(OctNum+ " convert to binary is "+ biArray[OctNum]);

					System.out.println();
				}
				else if(OctNum>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

Recommended Answers

All 9 Replies

Hi,
May someone let me know of how do I convert more than 1 octal number to binary(ex: 34526 or even more) using an array? My professor said something like I first have to isolate each octal number, then read it as string or char to each equivalent binary number (group of 3: 000, 001, ..., 111).

Basically, the program here about covert the Octal number to Binary number which Octal number will be input by user from DOS window. I can only convert 1 single Octal number at a time and prompt "invalid" every time it's larger than 7. Thanks

/*
*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
        int OctNum;

        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: ");
                OctNum = input.nextInt();

                if(OctNum <=7)   
                {   
                    System.out.println(OctNum+ " convert to binary is "+ biArray[OctNum]);

                    System.out.println();
                }
                else if(OctNum>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
Member Avatar for hfx642

Take your input as a string instead of an integer.
Traverse the string one character at a time (in a loop).
Convert that character to an integer and use IT as your array index.
"34526" is;
"3" converted to 3. biArray [3] is "011"
"4" converted to 4. biArray [4] is "100"
...

Could you give an example of what your expected output would be for the input of: 34526?

Think about how you would convert a String of "34526" to a decimal value in an int variable. 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.

OK, I know that my loop doing something wrong here. when I run it, it gave me long result and only 1 number being read to the same 010, like this:

Octal to Binary Conversion
Enter the Octal Number:
1
1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

Do you want to enter another Octal number? Y for yes or N for No:

y
Octal to Binary Conversion
Enter the Octal Number:
123
123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

Do you want to enter another Octal number? Y for yes or N for No:

May you help?! Thanks
Octal to Binary Conversion
Enter the Octal Number:
1
1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

1 convert to binary is 010

Do you want to enter another Octal number? Y for yes or N for No:

y
Octal to Binary Conversion
Enter the Octal Number:
123
123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

123 convert to binary is 010

Do you want to enter another Octal number? Y for yes or N for No:

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);
					c++;
					for (j=0; j<c; j++)
					{
						//convert character to integer (ex: "1" to 1)don't know how???
						n = Character.getNumericValue(c);
						if(n <=7)	
						{
						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

Could you give an example of what your expected output would be for the input of: 34526?

ok!
something like: after user input in 34526, the output offcourse will be group of 3 with no space in between:
011100101010110

Here is a way to verify your code for any given octal number:

// Code to verify results: octal to binary
      String OctVal = "34526";  // This is the octal number to convert
      int intVal = Integer.parseInt(OctVal, 8);
      System.out.println("octal number: " + OctVal + ", as decimal=" + intVal
                          + ", as binary=" + Integer.toBinaryString(intVal));
      // octal number: 34526, as decimal=14678, as binary=11100101010110

Looking at your code I have no idea what it is doing. Can you explain step by step what the code is supposed to do? For example why do you assign values to the biArray elements two times?

I suggest you start over, think thru the problem, design the code to solve it and then type it in.

well,
If you view my post from the beginning you should understand why I have assigned the variable to the biArray twice. I added the second variable to my last post because I wanted to show it for suggestion on what to drop.

With your given code here, first,you only assigned to one particular Octal number while the program needs to convert to whatever the input octal is from the user. Second, I was not allow to use the Integer.toBinaryString method as we have to convert them ourself, not the system. (not sure I explained it clear enough here?)

why I have assigned the variable to the biArray twice

No, I don't understand at all.

The code I posted was for getting the correct results to verify that your code is working properly, not as a way to solve your problem.

Specific questions:
What are these three lines of code supposed to do?

c = OctStr.charAt(0);
	  c++;
	  for (j=0; j<c; j++)

c is a char not an int.
Print out its value by casting it to an int to see why you loop goes around over 40 times.

What is the for(j...) loop supposed to do? Get rid of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.