# Text Being Written Over The Top Of Each Other

• 06-14-2006, 11:58 PM
crawf
Text Being Written Over The Top Of Each Other
Hi everyone,

I have a simple applet to display the perfect numbers from 1-500...but when the numbers that are considered to be perfect are displayed, they are written over the top of each other...

Ive used a simple for loop, but it doesnt seem to help...does anyone have any ideas as to what is going on?

Thanks very much!

Code:

```import java.applet.*; import java.awt.*;   public class PerfectNumbers extends Applet {       int maxNum=500;     int perfectCount=0;       public void paint (Graphics g){     {                for(int ctr=maxNum;ctr>=2;ctr--)         {             int sum = 0;             int sum2 = 0;             for(int ctr2 = ctr; ctr2>=2; ctr2--)             {                 if(ctr%ctr2==0)                 {                     sum = sum+(ctr/ctr2);                     sum2 = ctr/ctr2;                 }             }         if(ctr==sum)         {             for (int i = 1; i<=4; i++)             {                 g.drawString(""+ctr,20,120+i*20);                 i++;             }             perfectCount++;             }                    }     }     g.drawString("There are "+perfectCount+" Perfect numbers from 1-" +maxNum,20,60);    } }```
-Crawf
• 06-16-2006, 07:46 AM
graviton
it's because you have two for loops.
the inner loop for (int i = 1; i<=4; i++) which does the drawing is
initialized allways equal when the outer loop iterates.
try to draw the strings dependent on the variable of the outer loop,
eg g.drawString(""+ctr,20,120+ctr*20); or sth like that.
• 06-16-2006, 08:04 AM
crawf
ah right! i see what you mean now! :) im pretty stupid! i was looking at it for ages trying to figure out that tiny little thing! I guess i must have been looking TOO hard!

Thanks for your help graviton! you've helped lots, all is good now! :)

-Crawf