-6

I need to do find values 10 different times or until the value of difference is found twice. Im not really sure how to break the loop if found.

Here goes the code:

public class Algorithm {
    private static int small; //global field so it is usable in zeros method

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int number = 0;
        // object array to use collections class
        Integer [] digit = new Integer [4]; 
        // loop for handling 10 different numbers
        for (int index=0; index<10; index++){ 
            number = random();
            String smaller =""; String bigger=""; // strings used to display zeros/ easier processing 
            for (int i = 3; i >= 0; i--) {
                digit[i] = number % 10;
                number /= 10;   
            }
            // prints initial random number
            System.out.println(digit[0] + " " +digit[1] + " " + 
            digit[2]+ " "+ digit[3] + " Random Number");
            // sorts the digits increasingly
            Arrays.sort(digit);
            // adds the numbers to the smaller string
            for (int i=0; i <digit.length; i++){
                smaller += digit[i]; 

            }
            // convert string to int
            int small = Integer.parseInt(smaller); 
            String zerosNr = null;
            zerosNr = zeros();
            System.out.printf(" smaller " +zerosNr, small );
            // Reverse sort order and adds results to bigger for displaying
            Arrays.sort(digit, Collections.reverseOrder());
            for (int i=0; i < digit.length; i++){
                bigger += digit[i];
            }
            int big = Integer.parseInt(bigger);

            System.out.println("bigger " + big);
            int difference = 0;
            int [] copy;
            copy = new int[11];
            difference = big - small;
            copy[index] = big - small;
            // here i tried to do it 
            System.out.println( index + " Difference "+ difference);
            if (difference == copy[index+1]) 
                break;
        }
    }
    //method that creates random numbers
    public static int random(){
        final int n = 9999;
        Random rad = new Random();
        int number = rad.nextInt(n);
        return number;

    }
    //method that adds zeros where necesarry to smaller
    public static String zeros(){
        String zerosNr = null;
            if (small < 1000)
                return " %04d\n ";
            else if (small < 100)
                return " %03d\n ";
            else 
                return " %02d\n ";

    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Tibi Tmbs
  • 11
  • 4
  • 3
    You might want to elaborate on the actual goal you're after, i.e. input, desired output etc. instead of just dumping code. Questions that immediately come to my mind: _Which_ loop? _Which_ value? What output is required? etc. – Thomas Nov 18 '15 at 15:57
  • So, down where it says, "here i tried to do it", it looks like you're trying to find the same value for `difference` twice in a row. Is that what you're after? – Erick G. Hagstrom Nov 18 '15 at 16:01
  • sry its my first time posting a question so yeah.. so basically i want to break the big loop ( loops 10 times ) if any value of variable difference is equal to another value of difference on future iterations of the loop – Tibi Tmbs Nov 18 '15 at 16:02
  • So to be clear, you want to break out of the outer for loop from within a nested for loop? – Nick Allen Nov 18 '15 at 16:13
  • yes i want to break out of the outer loop from within itself – Tibi Tmbs Nov 18 '15 at 16:14
  • Possible duplicate of [How to break multiple foreach loop?](http://stackoverflow.com/questions/551578/how-to-break-multiple-foreach-loop) – Nick Allen Nov 18 '15 at 16:27
  • OP doesn't seem to be trying to break out of nested loops. At the point of his current `break` he's only in one level deep. – Erick G. Hagstrom Nov 18 '15 at 16:34

2 Answers2

0

A problem with your approach is that you're trying to break based on "future" values, i.e. when you compare difference == copy[index+1] you have not yet populated copy[index+1]. You just haven't gotten that far yet.

Instead, you should be comparing difference to past values of copy. And you'll have to compare each difference to all preceding values of copy. So something like:

boolean iShouldBreak = false;
for (int j = 0; j < index; j++) {
    if (difference == copy[j]) {
        iShouldBreak = true;
    }
}
if (iShouldBreak)
{
    break;
]
Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • it does not seem to work but thanks for pointing out the the error in my logic ill try going from there – Tibi Tmbs Nov 18 '15 at 16:27
  • Ok, good luck. If you can isolate your problem better feel free to ask again. Probably best to make it another separate question. Try to provide a Minimal, Complete, Verifiable Example: http://stackoverflow.com/help/mcve – Erick G. Hagstrom Nov 18 '15 at 16:40
0

You'll want to use a label:

For example:

OUTERFOR: for(Object1 object1: objects){
  for(Object2 object2: object1.getMoreObjects()){
    if( object2.isWhatever()) {
      break OUTERFOR;
    }
  }// inner for loop ends here
}// outer for loop ends here
Nick Allen
  • 917
  • 11
  • 24