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