Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with arrays program in java

 
Jr Winston
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi 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, it loops the binary equivalent. 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');

}
}
 
James E Baker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jr....

First... to save you some grief when posting, make sure to use "code" button when posting code on the forum to make it easier to read and reply to.

I'm not 100% sure I know exactly how you want your output to look, but I have four suggestions for you to try.

1. At the beginning, you have the line "int num = 0;" for no apparent reason. It is never used in your code.
2. You have a line "c = OctalNum.charAt(0); " which I think you should change to "c = OctalNum.charAt(i); "
3. What is the "for (j=1; j < c; j++) " about? I would completely delete that from your code. It doesn't seem to serve a purpose and "c" is a char value not an int.
4. Change your output to "System.out.println(n+ " converted to binary is "+ binaryArray[n]); "

Below shows the changes I made....


My output is....

Octal to Binary Conversion
Enter the Octal Number:
0123
0 converted to binary is 000
1 converted to binary is 001
2 converted to binary is 010
3 converted to binary is 011
Do you want to enter another Octal number? Y for yes or N for No:
 
Jr Winston
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks! i understand it now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic