-
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
-
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.
-
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();
}
}
-
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);
-
well that line just crashes my applet...
-
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-17-2006 at 11:46 PM.
-
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
-
*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
-
no prob. learning by doing, trial and error. now you can try using a jlist or jtextarea
Similar Threads
-
Replies: 0
Last Post: 02-21-2006, 08:47 PM
-
By Developer Express in forum dotnet.announcements
Replies: 0
Last Post: 03-21-2002, 07:03 PM
-
Replies: 3
Last Post: 08-30-2001, 11:45 AM
-
By Jeff Binnig in forum VB Classic
Replies: 1
Last Post: 06-16-2000, 04: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks