Hello all. I have to write a java program that allows a user to enter an octal digit and output the binary equivalent for as many numbers as the person wishes using a direct access method.

The error is that even though it compiles, the answer (the binary number) continuously loops then it stops. The code goes as follows:

import java.util.Scanner; // initialize scanner class for input
public class Arrays
{
public static void main(String[] args)
{
int num = 0;
String OctalNum;
// declare array to hold 7 octal numbers and convert to binary numbers
int i;
int j;
int n;

char c; //to hold each number of input octal

char repeat; //to hold yes or no

//for numbers 1 to 7, make a binary equivalent

String binaryArray[]={"000", "001", "010", "011", "100", "101", "110", "111"};


do
{


Scanner input = new Scanner(System.in);
System.out.println("Octal to Binary Conversion");

System.out.println("Enter the Octal Number: ");
OctalNum = input.nextLine();


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

c = OctalNum.charAt(0);

for (j=1; j < c; j++)
{
//convert character to integer (ex: "1" to 1)
n = Character.getNumericValue(c);
if(n <8)
{
binaryArray[0] = "000";
binaryArray[1] = "001";
binaryArray[2] = "010";
binaryArray[3] = "011";
binaryArray[4] = "100";
binaryArray[5] = "101";
binaryArray[7] = "111";

System.out.println(OctalNum+ " converted to binary is "+ binaryArray[n]);

// System.out.println();

}//end if
else if(n>7)
System.out.println( "Not a valid octal number!");

}

}

// prompt for input more OctalNum
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');

}
}

continuously loops then it stops

What values of what variables will allow the program to continuously loop?
Try debugging your code by Adding some printlns inside of the loop to display the value of the variable that controls the loop's execution.

what I meant to say was that after to enter your octal number (eg. "3"), the newly converted binary number repeats a stack of lines a number of times (eg. "3 converted to binary is 011") and then ends at the "Do you want to enter another Octal number? Y for yes or N for No:

Please show the output from when you execute the program.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Did you try what I suggested?
What values of what variables will allow the program to continuously loop?
Try debugging your code by Adding some printlns inside of the loops to display the value of the variables that controls the loops' execution.

import java.util.Scanner; // initialize scanner class for input
   public class Arrays
   {
      public static void main(String[] args)
      {
         int num = 0;
         String OctalNum;       
        	// declare array to hold 7 octal numbers and convert to binary numbers
         int i;
         int j;
         int n;
      	
         char c; //to hold each number of input octal

         char repeat; //to hold yes or no
      	
      	// for numbers 1 to 7, make a binary equivalent
			
	String binaryArray[]={"000", "001", "010", "011", "100", "101", "110", "111"};

      
        do
        {
         
                     
            Scanner input = new Scanner(System.in);
            System.out.println("Octal to Binary Conversion");
         
            System.out.println("Enter the Octal Number: ");
            OctalNum = input.nextLine();
         
                     
         // make a loop to read as many numbers as the input octal has
            for (i=0; i < OctalNum.length(); i++)
            {
            //read string by character then convert to an integer
               
	 c = OctalNum.charAt(0);
            
              for (j=1; j < c; j++) 				             
               {                  
                System.out.println(OctalNum+ " converted to binary is "+ binaryArray[n]);
                     
                     System.out.println();
                  
                  }//end if
                  if(n>7)
                     System.out.println( "Not a valid octal number!");
               
               }
            

         
         // prompt for input more OctalNum
            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');
      
      }   
   }

Have you solved your problem? I don't see any questions on your last post.

If not:
Did you try what I suggested?
What values of what variables will allow the program to continuously loop?
Try debugging your code by Adding some printlns inside of the loops to display the value of the variables that controls the loops' execution.
System.out.println("j=" + j);

it keeps saying that variable n has not been initialized.

for (i=0; i < OctalNum.length(); i++)
				
System.out.println("n=" + n);

            {
            //read string by character then convert to an integer
               
				 c = OctalNum.charAt(0);

it keeps saying that variable n has not been initialized.

The compiler can not see where you have assigned a value to the variable: n
You need to assign n a value before you try to use it.

I made some corrections to it. Here's the output, and I still can't find what's causing it to print out the variable to repeat.

Octal to Binary Conversion
Enter the Octal Number:
4
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
j=10
j=11
j=12
j=13
j=14
j=15
j=16
j=17
j=18
j=19
j=20
j=21
j=22
j=23
j=24
j=25
j=26
j=27
j=28
j=29
j=30
j=31
j=32
j=33
j=34
j=35
j=36
j=37
j=38
j=39
j=40
j=41
j=42
j=43
j=44
j=45
j=46
j=47
j=48
j=49
j=50
j=51
4 converted to binary is 100

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

n

Here's the code:

// Lab 11: Arrays
// File: Arrays.java
// Designed by: John Shin
// 08/05/2011

   import java.util.Scanner; // initialize scanner class for input
   public class Arrays
   {
      public static void main(String[] args)
      {
       
         String OctalNum;       
        	// declare array to hold 7 octal numbers and convert to binary numbers
         int i;
         int j;
			int n;          
			 	
         char c; //to hold each number of input octal

         char repeat; //to hold yes or no
      	
      	// for numbers 1 to 7, make a binary equivalent
			
			String binaryArray[]={"000", "001", "010", "011", "100", "101", "110", "111"};

      
        do
        {
                            
            Scanner input = new Scanner(System.in);
            System.out.println("Octal to Binary Conversion");
         
            System.out.println("Enter the Octal Number: ");
            OctalNum = input.nextLine();
         
                     
         // make a loop to read as many numbers as the input octal has
            for (i=0; i < OctalNum.length(); i++)				
            {
            //read string by character then convert to an integer
               
	c = OctalNum.charAt(0); 
            
              for (j=1; j < c; j++) 	
	       System.out.println("j=" + j); 			             
               {  
									 					
		 // convert character to integer
		n = Character.getNumericValue(c);
		if (n <8)
					 {
					 						           
                System.out.println(OctalNum+ " converted to binary is "+   binaryArray[n]);
                     
                     System.out.println();
                  
                  }//end if
						
                  if(n>7)
                     System.out.println( "Not a valid octal number!");
               
               }
            
				}
         
         // prompt for input more OctalNum
            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');
      
      }   
   }

I still can't find what's causing it to print out the variable to repeat

What is the value of c? Not the character but the ASCII value.
For example execute this:

System.out.println("4=" + (int)'4');

You are using the char c as an int in your if test:

for (j=1; j < c; j++)

So j will increment up to about 50

but why is j incrementing to about 50?

what does the following code do?

System.out.println("4=" + (int)'4');

I changed the following code, replacing the 'c' to 1:

for (j=1; j < 1; j++)

the output was when I inputted '1':

Octal to Binary Conversion
Enter the Octal Number:
1
1 converted to binary is 001

for (j=1; j < c; j++)

What is the numeric value of c? c's value is what will stop this loop. I think the value of c is near 50.
Execute this code to see what the numeric value of '4' is:

System.out.println("4=" + (int)'4');

The above code prints out the numeric value of '4'.

When you read a char from the keyboard, its numeric value is not the same as the digit it represents. '4' represents a value of four, but its numeric value is 52.
All the keys on the keyboard will give your program a different numeric value. There are over 100 different values that you can type in.

thank you for clarifying this.

Please start your own thread.

Your biggest problems are right here:

c = OctalNum.charAt(0);

				for (j = 1; j < c; j++)
					System.out.println("j=" + j);
				{

I see at least two bugs in the lines above.

Hint: The reason 'j' counted from 1 to 51 is that 51 is the ASCII/UNICODE value for the character '4' (which you entered).

Hint: What does for (j = 1; j < c; j++) do for you? Why do you think you need it?

Once you get past that...

Hint: Why does it keep converting the first digit of the number to octal, ignoring the other digits?

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.