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: Error with OR operator

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Error with OR operator

    Hi, I know this is going to be SO simple when its pointed out but I've been working on this for the past 3 hours and I just can't understand it. I am VERY new to Java and I am trying to solve the 1st problem from project Euler. The problem is: To add all the natural numbers below 1000 that are divisable by 3 or 5. The following code determins whether the number is divisible by 3 and adds i to the total each time it finds a one. This part of the code works perfectly and I get the expected outcome. I can also change the value to 5 and that works accordingly.

    class EulerFirstProject {
    	public static void main(String[] args) {
     
    		int total = 0;
     
    		for (int i = 0; i < 1000; i++) {
    			if (i % 3 != 0) {
    				continue;
    			}
    			total += i;
    		}
     
    		System.out.println(total);
    	}
    }

    So give that this works perfectly I figured I would just be able to add an OR operator to determine whether i is divisible by 3 OR 5:

    class EulerFirstProject {
    	public static void main(String[] args) {
     
    		int total = 0;
     
    		for (int i = 0; i < 1000; i++) {
    			if ((i % 3 != 0) || (i % 5 != 0)) {
    				continue;
    			}
    			total += i;
    		}
     
    		System.out.println(total);
    	}
    }

    However for some reason this simply returns the value 49500. Witch is wrong. I have tried replacing 1000 with 10 which should give me a value of 23 but I get 0. Any pointers would be hugely appreciated.

    Thanks
    Last edited by tarkal; September 4th, 2011 at 02:03 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error with OR operator

    To figure out how to write the conditions correctly, write a small simple program with a loop with the if test inside. Add printlns to show when the conditions are true and when they are not and to show the value of i for both those cases.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Error with OR operator

    Ohhhhhhhhh, I can't beleive it... I'm so simple!!! Thanks very much for the quick feedback. I'm so sorry for such a stupid post

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error with OR operator

    No problem. Lots of programmers have problems with != and || together.
    A quick short program to printout what is happening and the light comes on.

Similar Threads

  1. about Operator Precedence
    By degar in forum Java Theory & Questions
    Replies: 6
    Last Post: August 12th, 2011, 01:57 AM
  2. Sobel Operator (Edge Detection)
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 18th, 2011, 06:21 PM
  3. how to extract variables,keywords,operator...
    By Nanda in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2009, 10:19 PM
  4. Operator Problem
    By KevinGreen in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 2nd, 2009, 09:50 AM
  5. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM