DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Text Being Written Over The Top Of Each Other

1. Registered User
Join Date
May 2006
Posts
28

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

2. Registered User
Join Date
Feb 2006
Location
Cologne - Germany
Posts
271
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.

3. Registered User
Join Date
May 2006
Posts
28
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

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise