Limiting numbers printed, under a user given value


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Limiting numbers printed, under a user given value

Hybrid View

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

    see what im talking about!

    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);
            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

  2. #2
    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. #3
    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);
            add(label);
            add(inputField);
            add(maxField);
            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 && 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. #4
    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. #5
    Join Date
    May 2006
    Posts
    28
    well that line just crashes my applet...

  6. #6
    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
        int maxNum;                                         // Value retrieved from input TextField
        int maxDisplay;                                     // Value retrieved from max TextField
        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
            add(label);                                                 // Add to GUI
            add(inputField);                                            // Add to GUI
            add(maxField);                                              // Add to GUI
            add(perfectButton);                                         // Add to GUI
            inputField.addActionListener(this);                         // Add to ActionListener
            perfectButton.addActionListener(this);                      // Add to ActionListener
            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();
        }
    }

    Thanks for your help!

    -Crawf
    Last edited by crawf; 06-18-2006 at 12:46 AM.

  7. #7
    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:
    int maxNum = 500; // Value retrieved from input TextField
    int maxDisplay = 2; // Value retrieved from max TextField

  8. #8
    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. #9
    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

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2006, 09:47 PM
  2. Bring Your User Interface to the Printed Page with the XtraPrinting Library
    By Developer Express in forum dotnet.announcements
    Replies: 0
    Last Post: 03-21-2002, 08:03 PM
  3. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 12:45 PM
  4. Replies: 1
    Last Post: 06-16-2000, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center