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

Thread: Dividing inside a for loop. Lolwtf?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dividing inside a for loop. Lolwtf?

    I have a for loop which looks like so: for(int x = 0; x<=1000; x++){ System.out.println(x/3); }

    Now for some insanely strange reason, when I run the code, it repeats the awnsers however many times im dividing by. So lets say its 3 it prints 000 111 222 and so on, 4 is like 1111 2222 3333 and etc. How in the world do I fix this, i've tried a bunch of different ideas, nothing.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Dividing inside a for loop. Lolwtf?

    You're using integer division, which truncates the results to an integer. You want to promote the expression type to floating point.

    System.out.println(x / 3.); // note the . after the 3
    System.out.println((double) x / 3); // same result, explicitly cast x to double and 3 gets implicitly promoted

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Dividing inside a for loop. Lolwtf?

    for(int x = 0; x <= 1000; x++) { 
        System.out.println(x/3); 
    }

    Sorry your code is much easier to read this way. Helloworld hit the nail on the head.

    But in more detailed wording, because you are dividing x (an integer) by 3 (an integer also), your answer is going to be... you guessed it, and integer. To truncate a value is to basically shorten a number by dropping a digit(s).

    In your code, when you print out x / 3, it's going to print the truncated integer from the expression x / 3 because the type Int can't hold floating point values.

    So as helloworld said you need to cast your output to a float point, and you can do that by adding a period after 3, or casting the entire expression to a double.

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dividing inside a for loop. Lolwtf?

    Quote Originally Posted by helloworld922 View Post
    You're using integer division, which truncates the results to an integer. You want to promote the expression type to floating point.

    System.out.println(x / 3.); // note the . after the 3
    System.out.println((double) x / 3); // same result, explicitly cast x to double and 3 gets implicitly promoted
    Thanks a bunch. Now my next question, i'm asking this because i'm doing the project euler first problem, and I am COMPLETLY mind boggled at the awnser. It asks what is the sum of the multiples of 3 or 5, under 1000.

    so thats 3+6+9 until the multiple is right under 1000 (995 and 999) I added all these up, and the awnser doesn't equal the awnser on project euler. What am I doing wrong? It makes me feel like they are the ones who are incorrect.

    This is my code:

    package sumsof3sand5s;
     
    public class app {
    public static int a;
    	public static  int x;
    	public static int b;
    	public static int c;
     
    	public static void main(String[] args) throws InterruptedException {
     
    		for(x = 0;x<1000;x++){
    			if(x%3==0){
    				a=a+x;
    				System.out.println(x);
    			}
    		}
     
    		Thread.sleep(2000);
    		System.out.println("");
    		for(int z = 0;z<1000;z++){
    			if(z%5==0){
    				b=b+z;
    				System.out.println(z);
    			}
    		}
     
     
    		System.out.println(a+b);
     
     
    	}

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Dividing inside a for loop. Lolwtf?

    Quote Originally Posted by samy109 View Post
    so thats 3+6+9 until the multiple is right under 1000 (995 and 999) I added all these up, and the awnser doesn't equal the awnser on project euler. What am I doing wrong? It makes me feel like they are the ones who are incorrect.
    Post your progress and I will be able to assist you, but we must locate the problem.

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dividing inside a for loop. Lolwtf?

    Quote Originally Posted by AlexHail View Post
    Post your progress and I will be able to assist you, but we must locate the problem.
    This is my code:

    package sumsof3sand5s;
     
    public class app {
    public static int a;
    	public static  int x;
    	public static int b;
    	public static int c;
     
    	public static void main(String[] args) throws InterruptedException {
     
    		for(x = 0;x<1000;x++){
    			if(x%3==0){
    				a=a+x;
    				System.out.println(x);
    			}
    		}
     
    		Thread.sleep(2000);
    		System.out.println("");
    		for(int z = 0;z<1000;z++){
    			if(z%5==0){
    				b=b+z;
    				System.out.println(z);
    			}
    		}
     
     
    		System.out.println(a+b);
     
     
    	}


    What it does, is first it counts up the numbers 3, 6 - till 999, then gets the sum of all those numbers (166833). then it does the same for 5. 5-10, all the way to 995 (99500). Then it adds these two values up (266333). I do not see how this could be incorrect, as it does exactly what's asked.

    --- Update ---

    Nevermind I figured it out.

  7. #7
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Dividing inside a for loop. Lolwtf?

    You must compensate for those numbers that are counted twice.

    Ex. 30 is a multiple of 3, and 5. So when determining if the iteration is a multiple of either 3 or 5, it will add that number to the sum twice.

Similar Threads

  1. how to call a string inside a while loop
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 07:25 AM
  2. New to Java, need help with arrays inside a loop please!
    By Darkaudicate in forum Loops & Control Statements
    Replies: 1
    Last Post: November 21st, 2012, 06:32 PM
  3. Can I have an if statement inside a while loop? or something similar?
    By ColeTrain in forum Java Theory & Questions
    Replies: 3
    Last Post: October 3rd, 2012, 06:01 PM
  4. Timer inside loop
    By dove in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2012, 10:57 AM
  5. Calculator dividing by 0 problem.
    By BITmixit in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 18th, 2012, 09:06 PM