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...
see what im talking about!

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);
        add(label);
        add(inputField);
        add(perfectButton);
        inputField.addActionListener(this);
        perfectButton.addActionListener(this);
        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

Member Avatar for DaSogo

Try storing your perfect values in an array and either allow the user to view the requested number of values from the beginning of the array or from the end of it. It's an implementation issue! It's your call!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.