Skip to content
Advertisement

How to expand a number to match another in Java

I’m given a number for example 319 and i need to write code that will break out a number to 3192310, for example. I’ll try to explain how it needs to be done and what i’ve tried.

Explanation: (given two inputs: 319 and 3192310) So the program starts by appending 0 to 9 at the end of 319, and compares that fourth digit to the fourth digit in 3192310. In this case, that digit would be “2”. At the line 3192, the program appends 0 to 9 at the end of 3192, and compares the fifth digit to the fifth digit in 3192310. In this case, that digit would be “3”. the program continues until it reaches the end and a “bingo” string is appended to 3192310.

Here’s an example of what the output should be:

319
3190
3191
31920
31921
31922
319230
3192310 bingo  
3192311
3192312
3192313
3192314
3192315
3192316
3192317
3192318
3192319
319232
319233
319234
319235
319236
319237
319238
319239
31924
31925
31926
31927
31928
31929
3193
3194
3195
3196
3197
3198
3199

My attempt: So I had started by first taking the last 4 characters of the input from a JTextField and placing it in an array. Then I created a for loop with an if and else statement comparing the index of the for loop to the value of the array at a given index. If the index of the for loop matched the value of the array, called the funtion again (recursive function). See my code below:

 public JTextArea banalysis(JTextField npaInput, JTextField rcInput, JTextField ccInput, JTextField lInput, JTextField breakoutInput, JTextField newRcInput){

        //Store text field inputs into variables
        String number=numberInput.getText();
        String breakout=breakoutInput.getText();

        int breakoutLength= breakout.length();
        String breakoutNoNpa=breakout.substring(3,breakoutLength);

        int[] breakoutArray=new int[breakoutNoNpa.length()];
        for(int r=0;r<breakoutNoNpa.length();r++){
            breakoutArray[r]=Integer.parseInt(breakoutNoNpa.substring(r,r+1));
        }


        this.recursivePrint(0,breakoutArray, number);

        return rightOutputBText;
    }

public void recursivePrint(int index, int[] breakoutArray, String number) {
  //number=319 is a String
 for (int i=0; i<=9;i++) {
                if (i == breakoutArray[index]) {
                    index++;
                    number=(number+i);
                    recursivePrint(index, breakoutArray, number);
                } else {
                    rightOutputBText.append( number + i + "n");
                }
            }
        }//end of recursivePrint method

But this is my output:

319
3190
3191
31920
31921
31922
319230

I’ve gone through my code and I get how I got the output, I’m just not sure how to change it. I feel lke I need some sort of nested for loop but I’m not sure. Can anyone help me out. Thanks

Advertisement

Answer

I figured it out. Your code was originally throwing an IndexOutOfBoundsException because index was not being checked against breakoutArray.length - 1. After I fixed the exception, with if((index < breakoutArray.length - 1) && i == breakoutArray[index]), I was getting the following wrong output:

3190
3191
31920
31921
31922
319230
3192310 bingo
3192311
3192312
3192313
3192314
3192315
3192316
3192317
3192318
3192319
3192312 <-- this should be 319232
3192313 <-- this should be 319233
... and on and on

This took me a while to figure out, but it came down to altering number to number + i in this code:

if (i == breakoutArray[index]) {
    index += 1;
    number = number + i;
    recursivePrint(index, breakoutArray, number);
}

Using number = number + i sets number in that stack frame, as well as all subsequent recursive stack frames. This was remedied by passing number + i in the recursive call, via recursivePrint(index + 1, breakoutArray, number + i).

The complete, working, method code follows:

public void recursivePrint(int index, int[] breakoutArray, String number) {
    
    //number=319 is a String
    for (int i = 0; i <= 9; i++) {
    
        if ((index < breakoutArray.length - 1) && i == breakoutArray[index]) {
            recursivePrint(index + 1, breakoutArray, number + i);
        } else {
            
            if((index == breakoutArray.length - 1) && i == breakoutArray[index]) {
                rightOutputBText.append(number + i + " bingon");
            } else {
                rightOutputBText.append(number + i + "n");
            }
            
        }
    
    }
} //end of recursivePrint method

Output using 319 and 3192310 follows:

3190
3191
31920
31921
31922
319230
3192310 bingo
3192311
3192312
3192313
3192314
3192315
3192316
3192317
3192318
3192319
319232
319233
319234
319235
319236
319237
319238
319239
31924
31925
31926
31927
31928
31929
3193
3194
3195
3196
3197
3198
3199
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement