Limiting numbers printed, under a user given value

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Limiting numbers printed, under a user given value

#### Hybrid View

1. Registered User
Join Date
May 2006
Posts
28

## Limiting numbers printed, under a user given value

Hi again guys!

I was just wondering how to limit the number of values that are printed out on the screen from a users textField..

Like say, if a user put in 4, only 4 numbers would be printed...Im wanting to do this with my perfect numbers applet, so the user inputs how many perfect numbers to show...

At the moment, i can only figure out how to get the maximum number for the appelet to test numbers to...

Code:
```import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ExerciseTwoPerfectNumbers extends Applet implements ActionListener {

Label label;
TextField inputField;
Button perfectButton;
boolean notFirst;
int maxNum;
String perfect = "";
String perfect2 = "";

public void init() {
label = new Label("Enter the maximum number to test");
perfectButton = new Button("Solve");
inputField = new TextField("500", 5);
notFirst = false;
}

public void paint(Graphics g) {
int i = 1;
int perfectCount = 0;
if (notFirst) {
for (int count = 2; count <= maxNum; count++) {
int sum = 0;
for (int count2 = count; count2 >= 2; count2--) {
if (count % count2 == 0) {
sum = sum + (count / count2);
int sum2 = count / count2;
perfect = perfect + sum2 + "+";
}
}
if (count == sum) {
for (int ctr3 = 0; ctr3 < perfect.length() - 1; ctr3++) {
perfect2 = perfect2 + perfect.charAt(ctr3);
}
g.drawString("" + sum + " = " + perfect2, 20, 80 + i * 20);
perfect2 = "";
i++;
perfectCount++;
}
perfect = "";
}
g.drawString("There are " + perfectCount + " Perfect numbers from 1-" + maxNum, 20, 60);
}
}

public void actionPerformed(ActionEvent e) {
notFirst = true;
if (e.getSource() == perfectButton) {
maxNum = Integer.valueOf(inputField.getText());
}
repaint();
}
}```
Thanks everyone for having a look!

-Crawf

2. Registered User
Join Date
Feb 2006
Location
Cologne - Germany
Posts
271
why don't u use a jtextarea or jlist to display all of the numbers instead of drawing them?
using jcomponents will also enable you to use the jscrollpane, which gives your user the comfortable control to scroll down the list of generated numbers?

anyway: you can add another inputfield, like u use it for maxNum, where the user can enter the maxDisplay of numbers to display.
then you can check that within your outer loop:
for (int count = 2; count <= maxNum && perfectCount<=maxDisplay; count++)
this will stop the loop when any of the limits are reached.

3. Registered User
Join Date
May 2006
Posts
28
Hi, thanks for your reply! i might put in a textarea, that sound like a better idea, thanks!

the code info you gave me doesnt seem to quite work...have i done something wrong?

Code:
```import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ExerciseTwoPerfectNumbers extends Applet implements ActionListener {

Label label;
TextField inputField;
TextField maxField;
Button perfectButton;
boolean notFirst;
int maxNum;
int maxDisplay;
String perfect = "";
String perfect2 = "";

public void init() {
label = new Label("Enter the maximum number to test");
perfectButton = new Button("Solve");
inputField = new TextField("500", 5);
maxField = new TextField("4",5);
notFirst = false;
}

public void paint(Graphics g) {
int i = 1;
int perfectCount = 0;
if (notFirst) {
for (int count = 2; count <= maxNum && perfectCount<=maxDisplay; count++) {
int sum = 0;
for (int count2 = count; count2 >= 2; count2--) {
if (count % count2 == 0) {
sum = sum + (count / count2);
int sum2 = count / count2;
perfect = perfect + sum2 + "+";
}
}
if (count == sum) {
for (int ctr3 = 0; ctr3 < perfect.length() - 1; ctr3++) {
perfect2 = perfect2 + perfect.charAt(ctr3);
}
g.drawString("" + sum + " = " + perfect2, 20, 80 + i * 20);
perfect2 = "";
i++;
perfectCount++;
}
perfect = "";
}
g.drawString("There are " + perfectCount + " Perfect numbers from 1-" + maxNum, 20, 60);
}
}

public void actionPerformed(ActionEvent e) {
notFirst = true;
if (e.getSource() == perfectButton) {
maxNum = Integer.valueOf(inputField.getText());
maxDisplay = Integer.valueOf(maxField.getText());
}
repaint();
}
}```

4. Registered User
Join Date
Feb 2006
Location
Cologne - Germany
Posts
271
hm, can't see an obvious error.
what's the error?
did you try some debug informations, like:
Code:
```for (int count = 2; count <= maxNum && perfectCount<=maxDisplay; count++) {
System.out.println("count: "+count+" perfectCount: "+perfectCount+" maxNum: "+maxNum+" maxDisplay: "+maxDisplay);```

5. Registered User
Join Date
May 2006
Posts
28
well that line just crashes my applet...

6. Registered User
Join Date
May 2006
Posts
28
hmmm...ive been turning it around and trying a few things, but i cant see anything wrong with it! do you know what the problem is? i cant seem to pick out anything wrong!

anyway, here is the code if you want to look at it...

Code:
```import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ExerciseTwoPerfectNumbers extends Applet implements ActionListener {

Label label;                                        // Labels to show what the TextField's are for
TextField inputField;                               // TextField for user to enter maximum range to test perfect numbers
TextField maxField;                                 // TextField for user to enter maximum number of primes to display
Button perfectButton;                               // Button to display perfect numbers given the values in TextField's
boolean notFirst;                                   // Boolean to hide values until button is pressed
String perfect = "";                                //
String perfect2 = "";                               //

public void init() {
label = new Label("Enter the maximum number to test");      // New label telling user what to enter in TextField
perfectButton = new Button("Display");                      // New Button to display perfect numbers
inputField = new TextField("500", 5);                       // New TextField for user to input range
maxField = new TextField("2",5);                            // New TextField for user to input number of primes displayed
notFirst = false;                                           // Do not display when Applet starts
}

public void paint(Graphics g) {
int i = 1;                                                                                      //
int perfectCount = 0;                                                                           // How many numbers that are perfect
if (notFirst) {                                                                                 // If notFirst is true...
for (int count = 2; count <= maxNum && perfectCount<=maxDisplay; count++) {                 // Start testing perfect numbers at 2 as long as it is below maxNum
System.out.println("count: "+count+" perfectCount: "+perfectCount+" maxNum: "+maxNum+" maxDisplay: "+maxDisplay);
int sum = 0;                                                                            //
for (int count2 = count; count2 >= 2; count2--) {                                       //
if (count % count2 == 0) {                                                          //
sum = sum + (count / count2);                                                   //
int sum2 = count / count2;                                                      //
perfect = perfect + sum2 + "+";                                                 //
}
}
if (count == sum) {                                                                     //
for (int ctr3 = 0; ctr3 < perfect.length() - 1; ctr3++) {                           //
perfect2 = perfect2 + perfect.charAt(ctr3);                                     //
}
g.drawString("" + sum + " = " + perfect2, 20, 80 + i * 20);                         //
perfect2 = "";                                                                      //
i++;                                                                                //
perfectCount++;                                                                     //
}
perfect = "";                                                                           //
}
g.drawString("There are " + perfectCount + " Perfect numbers from 1-" + maxNum, 20, 60);    // States what operation is occuring
}
}

public void actionPerformed(ActionEvent e) {
notFirst = true;                                                    // Set notFirst to be true
if (e.getSource() == perfectButton) {                               // If button 'Display' is pressed...
maxNum = Integer.valueOf(inputField.getText());                 // Sets maxNum to the user input in range TextField
maxDisplay = Integer.valueOf(maxField.getText());               // Sets maxDisplay to the user input in perfect limit TextField
}
repaint();
}
}```

-Crawf
Last edited by crawf; 06-17-2006 at 11:46 PM.

7. Registered User
Join Date
Feb 2006
Location
Cologne - Germany
Posts
271
perhaps it's because both variables
maxNum and maxDisplay are not initialized properly:
by default they are set to 0. you should give them a reasonable default value, eg:

8. Registered User
Join Date
May 2006
Posts
28
*sigh* i cant believe that...it works fine now!!

Thank you VERY much for your help! im sorry im such an idiot for not noticing!

thanks again!

-Crawf

9. Registered User
Join Date
Feb 2006
Location
Cologne - Germany
Posts
271
no prob. learning by doing, trial and error. now you can try using a jlist or jtextarea

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