Results 1 to 4 of 4

Thread: Unknown error

  1. #1
    tbrowne03 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Unknown error

    Hi!
    So I've been working on a program to find the sum of all the multiples of 3 or 5 below 1000.
    Here is my code:
    Java Code:
    class AddAllMultiples{
       public static void main (String[] args){
          int addition = 0;
          for(int i=0; i <= 1000; i++){
             if((i % 3 == 0) || (i % 5 == 0)){
                System.out.println(i);
                i+=addition;
             }
          } 
          System.out.println("The total value is " + addition);
       }
    }
    Every single time I get that the total value is 0. I checked and "i" is working fine, but somehow it is not adding to addition and every time "addition" is 0. I have spent a very long time on this please help!
    Thanks!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Unknown error

    Try writing out the line "i+=addition" in full - ie without using the += operator.

    Also you might want to review exactly what the += operator does. There is a description (under "The Arithmetic Operators") in the Assignment, Arithmetic and Unary Operators page of Oracle's Tutorial. Read what it says carefully.

  3. #3
    tbrowne03 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Unknown error

    Ahh thank you. I had it completely backwards. It was doing i=i+addition, instead of addition=addition+i. (which is addition+=i)
    Thanks again!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Unknown error

    You're welcome - I'm glad you've got it sorted out.

Similar Threads

  1. pls. help on my unknown error
    By boybackup in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2011, 12:34 AM
  2. unknown error - really need help
    By smallmos1 in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 11:46 PM
  3. unknown html:form tag error.Pls help
    By yss1287b in forum New To Java
    Replies: 0
    Last Post: 12-14-2010, 11:31 PM
  4. Unknown NullPointerException Error...
    By ryanonnfire54 in forum New To Java
    Replies: 4
    Last Post: 11-16-2010, 07:23 PM
  5. Unknown error in Jtable...
    By blu_fox88 in forum AWT / Swing
    Replies: 2
    Last Post: 02-23-2010, 06:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •