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

Thread: increase while loop && var

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default increase while loop && var

    while ((x<y) &&
    				((red[0][x] != target) &&( red[1][x] != target) && (red[2][x] != target)
    				&& (red[3][x] != target) && (red[4][x] != target) && (red[5][x] != target) && 
    				(red[6][x]!= target) && (red[7][x]!= target) && (red[8][x] != target) && 
    				(red[9][x] != target))){
    					x++;

    The problem is red is a variable and i need to check all the values in the red row. i dont know red will have 10 vaules.

    any ideas? As im in a while loop i cant use a for loop here. x increases each time to check the columns, which works fine.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,483
    My Mood
    Hungover
    Thanks
    144
    Thanked 643 Times in 548 Posts

    Default Re: increase while loop && var

    Quote Originally Posted by Scotty View Post
    As im in a while loop i cant use a for loop here.
    Why not? What happened when you tried?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: increase while loop && var

    Break the problem down into pieces...you want to check several array values using logic, so to you cold just create a method that checks the array for you, returning the logic boolean your while loop needs.

  4. The Following 2 Users Say Thank You to copeg For This Useful Post:

    javapenguin (December 9th, 2010), Scotty (December 9th, 2010)

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: increase while loop && var

    while ((x<y) &&
    ((red[0][x] != target) &&( red[1][x] != target) && (red[2][x] != target)
    && (red[3][x] != target) && (red[4][x] != target) && (red[5][x] != target) &&
    (red[6][x]!= target) && (red[7][x]!= target) && (red[8][x] != target) &&
    (red[9][x] != target))){
    x++;

    You could always check like this
    Have another variable for the 0-9.
    int z =0;

    while ((x<y) && (red[z][x]!=target) && (z < red.length) &&(x < red[z].length))
    {
    x++;
    z++;
    }

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,483
    My Mood
    Hungover
    Thanks
    144
    Thanked 643 Times in 548 Posts

    Default Re: increase while loop && var

    Quote Originally Posted by javapenguin View Post
    while ((x<y) &&
    ((red[0][x] != target) &&( red[1][x] != target) && (red[2][x] != target)
    && (red[3][x] != target) && (red[4][x] != target) && (red[5][x] != target) &&
    (red[6][x]!= target) && (red[7][x]!= target) && (red[8][x] != target) &&
    (red[9][x] != target))){
    x++;

    You could always check like this
    Have another variable for the 0-9.
    int z =0;

    while ((x<y) && (red[z][x]!=target) && (z < red.length) &&(x < red[z].length))
    {
    x++;
    z++;
    }
    Why.

    Do you love giving incorrect answers? Did you test that at all?

  7. #6
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: increase while loop && var

    My problem is I dont see how to increase/decrease 0-9 because of the &&. Using a for could increase or decrease the number but not add or take away a term from the while loop.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: increase while loop && var

    You could always use a for loop.

  9. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: increase while loop && var

    It depends, if the while loop is supposed to end if just one of those conditions is false, then use a boolean

    boolean works = true;

    if in your for loop, a value equals target, you change works to false and then it'll exit the while loop.

  10. #9
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: increase while loop && var

    for loops dont remember the vars...... ie i how do i keep track of the position when it exits the loop?

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,483
    My Mood
    Hungover
    Thanks
    144
    Thanked 643 Times in 548 Posts

    Default Re: increase while loop && var

    Quote Originally Posted by Scotty View Post
    for loops dont remember the vars...... ie i how do i keep track of the position when it exits the loop?
    public class ForLoopTest {
       public static void main(String[] args) {
          int i;
          for(i = 0; i < 10; i++){
        	  //do something
          }
          System.out.println(i);
       }
    }

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Scotty (December 9th, 2010)

  13. #11
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: increase while loop && var

    strange, maybe its me, but my i always resets to 0..... even int i = 0; delcared outside....

  14. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,483
    My Mood
    Hungover
    Thanks
    144
    Thanked 643 Times in 548 Posts

    Default Re: increase while loop && var

    Quote Originally Posted by Scotty View Post
    strange, maybe its me, but my i always resets to 0..... even int i = 0; delcared outside....
    Can you post the code, in SSCCE form, that does that? It works just fine for me.

Similar Threads

  1. Increase Java Memory Allocation
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: July 15th, 2010, 02:34 PM
  2. Increase heap size; 4 GB wall?
    By BKB in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 13th, 2010, 08:55 PM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  5. Can we increase the stack size for JVM ?
    By prasanna in forum Java Theory & Questions
    Replies: 4
    Last Post: August 4th, 2009, 03:17 PM