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: if else skipping

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

    Unhappy if else skipping

    Hiya,

    I am tried so may be missing something, but I dont know why this piece from my code is not working. By the time it gets to here var=147, but it only implements the final else statement, ignoring the true var%7 == '0'. I have never included an equation in else before, but setting it as an external var makes no difference. Have tried less brackets (not that that matters here). 147/7=21. Never used mod remainder before. Thanks.

    if ( (var%10) == '0') {
    quo = 10;
    System.out.println("quo" +quo); 
    }
    else if ((var%9) == '0') {
    quo = 9;
    System.out.println("quo" +quo);
    }
    else if ((var%8) == '0')  {
    quo = 8;
    System.out.println("quo" +quo);
    }
    else if ((var%7) == '0') {
    quo = 7;
    System.out.println("quo" +quo);
    }
    else if ((var%6) == '0'){
    quo = 6;
    System.out.println("quo" +quo);
    }
    else if ((var%5) == '0'){
    quo = 5;
    System.out.println("quo" +quo);
    }
    else if ((var%4) == '0'){
    quo = 4;
    System.out.println("quo" +quo);
    }
    else if ((var%3) == '0'){
    quo = 3;
    System.out.println("quo" +quo);
    }
    else if ((var%2) == '0'){
    quo = 2;
    System.out.println("quo" +quo);
    }
    else  {
    System.out.println("quo not divisible");
    }


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

    Lightbulb Re: if else skipping

    Quote Originally Posted by Scotty View Post
    Hiya,

    I am tried so may be missing something, but I dont know why this piece from my code is not working. By the time it gets to here var=147, but it only implements the final else statement, ignoring the true var%7 == '0'. I have never included an equation in else before, but setting it as an external var makes no difference. Have tried less brackets (not that that matters here). 147/7=21. Never used mod remainder before. Thanks.

     		                                                       if ( (var%10) == '0') {
    										quo = 10;
    										System.out.println("quo" +quo); 
    										}
    										else if ((var%9) == '0') {
    										quo = 9;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%8) == '0')  {
    										quo = 8;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%7) == '0') {
    										quo = 7;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%6) == '0'){
    										quo = 6;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%5) == '0'){
    										quo = 5;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%4) == '0'){
    										quo = 4;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%3) == '0'){
    										quo = 3;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%2) == '0'){
    										quo = 2;
    										System.out.println("quo" +quo);
    										}
    										else  {
    										System.out.println("quo not divisible");
    										}
    By having the '0', you're comparing it to a char.

    get rid of the ' ' and see what happens.

  3. The Following 2 Users Say Thank You to javapenguin For This Useful Post:

    JavaPF (October 26th, 2010), Scotty (October 25th, 2010)

  4. #3
    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: if else skipping

    Quote Originally Posted by Scotty View Post
    Hiya,

    I am tried so may be missing something, but I dont know why this piece from my code is not working. By the time it gets to here var=147, but it only implements the final else statement, ignoring the true var%7 == '0'. I have never included an equation in else before, but setting it as an external var makes no difference. Have tried less brackets (not that that matters here). 147/7=21. Never used mod remainder before. Thanks.

     		                                                       if ( (var%10) == '0') {
    										quo = 10;
    										System.out.println("quo" +quo); 
    										}
    										else if ((var%9) == '0') {
    										quo = 9;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%8) == '0')  {
    										quo = 8;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%7) == '0') {
    										quo = 7;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%6) == '0'){
    										quo = 6;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%5) == '0'){
    										quo = 5;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%4) == '0'){
    										quo = 4;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%3) == '0'){
    										quo = 3;
    										System.out.println("quo" +quo);
    										}
    										else if ((var%2) == '0'){
    										quo = 2;
    										System.out.println("quo" +quo);
    										}
    										else  {
    										System.out.println("quo not divisible");
    										}
    147/7=21

    so 147%7 = 0;

    1 % 2 = 1.

    3 % 2 = 1;

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

    Wink Re: if else skipping

    yep sorry, just figured that out

Similar Threads

  1. need help prog skipping method
    By Pulse_Irl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2010, 08:57 AM
  2. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 07:55 PM