Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java for loop problem and out put is not coming

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java for loop problem and out put is not coming

    Hello everyone,
    I have a code bit where the program takes ages to complete for some reason and i have a quad processor.
    The code is below; "allPossibleCombinationsOutput" is a String and maxBuffer is an int but when i get it above 1500, the program responds after 10secs of lockup and i need the number to be above 200000. What can i do? Thanks for your time.


    for (int y = 0; y < maxBuffer; y++) {
                allPossibleCombinationsOutput += "" + (y + 1);
                for (int x = 1; x < 7; x++) {
                    allPossibleCombinationsOutput += "\t" + allPossibleGuesses[y][x];
                }
                allPossibleCombinationsOutput += "\n";    
            }
            jTextPane3.setText(allPossibleCombinationsOutput);


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: no response loop problem

    Hello thewonderdude and welcome to the Java Programming Forums.

    What does this code do exactly?

    Is it possible to post all your code so I can attempt to compile it?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no response loop problem

    public void allPossibleCombinations(int array3[][]) {
     
            boolean yes = true;
            int cn[] = new int[7];
            Random generator = new Random();
            int z = 0, i = 1;
     
            for (int dummy = 0; dummy < 7; dummy++) {
                cn[dummy] = 0;
            }
            while (z < maxBuffer) {
                if (array3[1][cn[1]] < array3[2][cn[2]] && array3[2][cn[2]] < array3[3][cn[3]] && array3[3][cn[3]] < array3[4][cn[4]] && array3[4][cn[4]] < array3[5][cn[5]] && array3[5][cn[5]] < array3[6][cn[6]]) {
     
                    allPossibleGuesses[z][1] = array3[1][cn[1]];
                    allPossibleGuesses[z][2] = array3[2][cn[2]];
                    allPossibleGuesses[z][3] = array3[3][cn[3]];
                    allPossibleGuesses[z][4] = array3[4][cn[4]];
                    allPossibleGuesses[z][5] = array3[5][cn[5]];
                    allPossibleGuesses[z][6] = array3[6][cn[6]];
     
                    z++;
                }
                i = generator.nextInt(6) + 1;
                if (cn[i] == 25) {
                    cn[i] = 0;
                } else {
                    cn[i]++;
                }
                while (yes) {
                    i = generator.nextInt(6) + 1;
                    if (cn[i] == 25) {
                        cn[i] = 0;
                    } else {
                        cn[i]++;
                    }
                    yes = generator.nextBoolean();
                 }
                yes = generator.nextBoolean();
            }
            for (int a = 0; a < maxBuffer; a++) {
                for (int b = 0; b < maxBuffer; b++) {
                    if (allPossibleGuesses[a][1] == allPossibleGuesses[b][1] && allPossibleGuesses[a][2] == allPossibleGuesses[b][2] && allPossibleGuesses[a][3] == allPossibleGuesses[b][3] && allPossibleGuesses[a][4] == allPossibleGuesses[b][4] && allPossibleGuesses[a][5] == allPossibleGuesses[b][5] && allPossibleGuesses[a][6] == allPossibleGuesses[b][6] && a != b) {
                        allPossibleGuesses[a][1] = 0;
                        allPossibleGuesses[a][2] = 0;
                        allPossibleGuesses[a][3] = 0;
                        allPossibleGuesses[a][4] = 0;
                        allPossibleGuesses[a][5] = 0;
                        allPossibleGuesses[a][6] = 0;
                    }
                }
            }
            allPossibleCombinationsOutput += "G#\tFirstP\tSecondP\tThirdP\tFourthP\tFifthP\tSixthP\n--------------------------------------------------------------------------------------------------------------------------\n\n";
    [COLOR="Red"]        for (int y = 0; y < maxBuffer; y++) {
                allPossibleCombinationsOutput += "" + (y + 1);
                for (int x = 1; x < 7; x++) {
                    allPossibleCombinationsOutput += "\t" + allPossibleGuesses[y][x];
                }
                allPossibleCombinationsOutput += "\n";    
            }
            jTextPane3.setText(allPossibleCombinationsOutput);
        }[/COLOR]

    For this method to work, i have to use a number around 1500 for `maxBuffer`.

    This is the whole method where the problem is. I'm not comfortable with java yet as you can see but the problem seems to be the from the part i sent earlier(shown here red). This method randomly increases 1 or more up to 6 numbers to create 6 numbered combination up to the limit of the "maxBuffer" which needs to be above 200000. Fills 0 to a combination if it has the same numbers with another one. Last part just prints the whole combination set.
    The (int array3[][]) parameter holds numbers for 6 places. i.e array3[1][25] means 25th number from the list of numbers for 1 st position.
    Thanks for the effort.

  4. #4

    Default Re: no response loop problem

    Please can you write allPossibleGuesses and array3 declarations and their initialization too ?
    Last edited by leandro; March 11th, 2009 at 01:35 PM.

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no response loop problem

    int allPossibleGuesses[][] = new int[maxBuffer][7];
    int[][] holder = new int[7][750];
    holder is sent to the method;
    allPossibleCombinations(holder);

    holder has int values sorted. holder[1][12]-->means 1st position 12th number. in the method i sent previously, it is being used as array3. The 'allPossibleGuesses' gets values from the previously mentioned method.

    thanks in advance

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no response loop problem

    hello,
    i put notifiers to check what is happening and at the code which i wrote at my first post, the program processes quite quickly up to y= 5000 where i can see a definite slowdown of performance when in 100000 i can actually count the System.out.println("alive and well "+y) results 'n the console.

     for (int y = 0; y < maxBuffer; y++) {
                allPossibleCombinationsOutput += "" + (y + 1);
                for (int x = 1; x < 7; x++) {
                    allPossibleCombinationsOutput += "\t" + allPossibleGuesses[y][x];
                }
               [COLOR="Red"] System.out.println("alive and well  "+y);[/COLOR]
                allPossibleCombinationsOutput += "\n";    
            }
     jTextPane3.setText(allPossibleCombinationsOutput);

    is there someway to speed up the process vastly or theres no hope for this?
    thanks

  7. #7

    Default Re: no response loop problem

    Hello, let me do some test but if you iterate over 200000 it will take time

  8. #8

    Default Re: no response loop problem

    Sorry for the delay. I did some test, you can do some optimizations in your code. But working with a big matrix will implicate lot of time.

    A simple opotimization is using StringBuffer instead of String. Try this code and see the difference:

                  private StringBuffer allPossibleCombinationsOutput = new StringBuffer();
    	      private String allPossibleCombinationsOutput2 = new String();
    	      private int maxBuffer = 1000;
    	     private int allPossibleGuesses[][] = new int[maxBuffer][7];
     
            public void bb() {
    		Date d = new Date();
    		for (int i = 0; i < maxBuffer; i++)
    			for (int j = 0; j < 7; j++)
    				allPossibleGuesses[i][j] = 22;
    		for (int i = 0; i < maxBuffer; i++)
    			for (int j = 0; j < 7; j++)
    				System.out.println(allPossibleGuesses[i][j]);
    		for (int y = 0; y < maxBuffer; y++) {
    			allPossibleCombinationsOutput2 += "" + (y + 1);
    			for (int x = 1; x < 7; x++) {
    				allPossibleCombinationsOutput2 += "\t"
    						+ allPossibleGuesses[y][x];
    			}
    			System.out.println("alive and well  " + y);
    			allPossibleCombinationsOutput2 += "\n";
    		}
    		System.out.print(allPossibleCombinationsOutput2);
    		long diff = new Date().getTime() - d.getTime();
    		System.out.println(diff + " ms");
    	}
     
    	public void aa() {
    		Date d = new Date();
    		for (int i = 0; i < maxBuffer; i++)
    			for (int j = 0; j < 7; j++)
    				allPossibleGuesses[i][j] = 22;
    		for (int i = 0; i < maxBuffer; i++)
    			for (int j = 0; j < 7; j++)
    				System.out.println(allPossibleGuesses[i][j]);
    		for (int y = 0; y < maxBuffer; y++) {
    			allPossibleCombinationsOutput.append("" + (y + 1));
    			for (int x = 1; x < 7; x++) {
    				allPossibleCombinationsOutput.append("\t"
    						+ allPossibleGuesses[y][x]);
    			}
    			System.out.println("alive and well  " + y);
    			allPossibleCombinationsOutput.append("\n");
    		}
    		System.out.print(allPossibleCombinationsOutput);
    		long diff = new Date().getTime() - d.getTime();
    		System.out.println(diff + " ms");
    	}

    If you run the 2 methods you will see the differences in time (diff in miliseconds). This is a simple way to check the run time with Date.

    About using a matrix of 20000 rows, it will take time and I don't think if it is anything to do.

    Hope this help you. Let me know how it goes please.

  9. #9
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no response loop problem

    almost made it to 500000 in 3 minutes but gave an error;

    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

    after alive and well 474214,
    very fast and much better then the last time considering it could get up to 2000
    thank you,

  10. #10

    Default Re: no response loop problem

    Try increasing the maximum allowed heap space as explained in the instructions at this page:
    How do I increase the heap space for the Java VM in MATLAB 6.0 (R12) and later versions? - 1-18I2C

    This is a sample using 328 Mb of ram:

    java -Xmx382M -jar myjar.jar

    java -Xmx382M myClass

    Best regards

Similar Threads

  1. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  2. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  3. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM
  4. Replies: 6
    Last Post: April 14th, 2009, 08:02 AM
  5. SOAP Message Factory response error
    By Buglish in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 6th, 2008, 01:42 PM