Display result in JTextArea
I am having a little problem with displaying the result of this program in a JTextArea, can anyone help me plzpublic class myNumbers // displays the numbers upto 1000 and the perfect numbers in that range
{
public static void main(String args[])
{
int a,b,c = 0;
int numb[]; // declaring array
numb = new int[1000]; // creating the array and giving the size
for (int i=0;i<numb.length;i++)
{
numb = i+1;
//System.out.println (numb); // no need to be displayed
}
for(a=0;a<numb.length;a++)
{
b = a-1;
c = 0;
while(b>0)
{
if(a%b == 0)c += b;
b--;
}
if((a == c)&&(a!=0))
System.out.println("This is a perfect number: "+a); // i would like to display this result in a JTextArea
}
}
}
Anyone help me plz
Hello ShriKrishna,
I've added the code to create a JFrame and a JTextArea within this frame.
Look at the line where you wanted to output to a JTextArea. I've also included an unformmated code for you to easily cut & paste :-)
import javax.swing.*;
import java.awt.event.*;
public class TestTextArea {
public static void main(String args[]) {
JFrame myframe = new JFrame();
myframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
myframe.setSize(500,500);
JTextArea mytextarea = new JTextArea();
myframe.getContentPane().add(new JScrollPane(mytextarea));
int a,b,c = 0;
int numb[]; // declaring array
numb = new int[1000]; // creating the array and giving the size
for (int i=0;i<numb.length;i++) {
numb[i] = i+1;
//System.out.println (numb); // no need to be displayed
}
for(a=0;a<numb.length;a++) {
b = a-1;
c = 0;
while(b>0) {
if(a%b == 0)c += b;
b--;
}
if((a == c)&&(a!=0)) {
System.out.println("This is a perfect number: "+a); // i would like to display this result in a JTextArea
mytextarea.append("This is a perfect number: "+a+"\r\n");
}
}
myframe.show();
}
}
Unformatted
==================
import javax.swing.*;
import java.awt.event.*;
public class TestTextArea {
public static void main(String args[]) {
JFrame myframe = new JFrame();
myframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
myframe.setSize(500,500);
JTextArea mytextarea = new JTextArea();
myframe.getContentPane().add(new JScrollPane(mytextarea));
int a,b,c = 0;
int numb[]; // declaring array
numb = new int[1000]; // creating the array and giving the size
for (int i=0;i<numb.length;i++) {
numb = i+1;
//System.out.println (numb); // no need to be displayed
}
for(a=0;a<numb.length;a++) {
b = a-1;
c = 0;
while(b>0) {
if(a%b == 0)c += b;
b--;
}
if((a == c)&&(a!=0)) {
System.out.println("This is a perfect number: "+a); // i would like to display this result in a JTextArea
mytextarea.append("This is a perfect number: "+a+"\r\n");
}
}
myframe.show();
}
}