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 4 of 4

Thread: Exiting array loop

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Exiting array loop

    Hey there!

    I'm working on specific method (reserve a plane seat), and I have created a 2D array to represent the seats. here is the method so far to reserve a seat in economy class:

    public static void reserveEC(int x[][]){
     
     
    	for (int i=0;i<15;i++){
    		for (int j=0;j<6;j++) {
    			if (x[i][j] == 0) {
    				x[i][j] = 1;				
    			break;
    		}
    	}
    	}
    }

    The problem is, I want it to find the next available seat, and set it to 1 (which I'm going to make mean "taken").

    I keep getting the output :

    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000
    100000

    and I only want ONE seat to be set to 1 in my 15 x 6 array... any suggestions?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,288
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Exiting array loop

    You don't give enough info to answer your question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Exiting array loop

    The break statement will only break out of the inner loop and return to the outer loop which then continues running. If you want the method to end then replace the break with a return statement.
    Improving the world one idiot at a time!

  4. The Following User Says Thank You to Junky For This Useful Post:

    Scorks (October 16th, 2013)

  5. #4
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Exiting array loop

    I'll try that, thanks!

Similar Threads

  1. Array and loop
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 16th, 2012, 04:04 AM
  2. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 02:23 PM
  3. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 04:58 AM
  4. Java Web Start app exiting with access denie (java.lang.RuntimePermission
    By sonaljain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 13th, 2011, 09:43 PM
  5. [SOLVED] frame are exiting at the same time
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: January 25th, 2010, 08:13 PM

Tags for this Thread