need help with arrays program in java

Discussion in 'Mac Programming' started by speedracer5, Aug 5, 2011.

  1. speedracer5, Aug 5, 2011
    Last edited by a moderator: Aug 5, 2011

    speedracer5 macrumors newbie

    Joined:
    Jul 17, 2011
    #1
    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, it continuously loops the binary equivalent then it stops. The code goes as follows:

    Code:
    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');
    
    }
    }
     
  2. chown33, Aug 5, 2011
    Last edited: Aug 5, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Think about exactly what the red-hilited code is doing. In particular, which char is being assigned to c.

    Then think about exactly what the blue-hilited code is doing. You should be able to explain what you expect to happen, so please post an explanation. If you can't explain what your code is doing, it suggests it's doing something you don't expect (or want).

    If you explain what you want to happen correctly (analysis), but don't code it correctly, that's one kind of design error: a bug, which can be corrected. If you don't analyze what should happen correctly, you can still code it correctly (perfect correspondence to the analysis), but the output will always be wrong because the analysis is wrong.


    Code:
    binaryArray[0] = "000";
    binaryArray[1] = "001";
    binaryArray[2] = "010";
    binaryArray[3] = "011";
    binaryArray[4] = "100";
    binaryArray[5] = "101";
    binaryArray[7] = "111";
    
    Why do this? You already initialized the array. You don't need to set new values. If you think you do, explain why you think so.



    BTW, learn to use print() or println() or printf() to examine values of interesting variables during the run of the program. It's one of the simplest debugging techniques. For example, print the value of c at each iteration. This is called "logging" or "debugging by printf".

    It would be even better to learn to use the debugger. Then you could set a breakpoint and examine variables, step line by line, etc.

    If you have no debugging techniques at all, then you won't get very far.
     

Share This Page