Hi, I hope someone can point me in the right direction.
I'll get it out of the way: Yes, I am a college student. No, I am not
looking for anyone to do my homework, just looking for help. I have been
reading this ng all semester, and found it very helpful. I just have a
stupid problem going on right now.
Here's the project:
We have to make a little frame with a text field, where the user inputs a
number, and a text area where a message is displayed. There are several
buttons on the frame: Enter, Average, Show Ascending, Show Descending,
Median, and High Number. We are to use an array to store up to 50 integers
the user enters.
Here's my problem:
My problem is that I can't figure out how to load the array. If I do it in
the Enter button event, then a new array is created each time an integer is
entered. I tested this by adding the current int to the int in the previous
cell. Oh, I am also storing the logical end of the array in the 0 cell. If
I try to load the array in the main, I get an error: Non-static method
load_aNum cannot be referenced from a static context. We had a project
previously where I used two arrays (pre-filled, not using input numbers),
and I loaded it in the main, and it worked fine. Don't know why this won't
work.
What I have so far (I've just been concentrating on getting the user numbers
to enter into the array. I'll worry about the rest of it later):
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.util.Arrays.*; //thought I might need this later for sorting
the array
public class khNumberDisplayer extends Frame implements ActionListener
{
private Button btnEnter, btnAvg, btnAscend, btnDescend, btnMedian,
btnHigh;
private TextField tfNum1 = new TextField (5);
private TextArea taMessage = new TextArea();
private int[] aNum;
private int lastSub =0;
/** Creates a new instance of khNumberDisplayer */
public khNumberDisplayer()
{
setTitle ("Number Displayer by Kelly");
setLayout (new FlowLayout());
add (new Label ("NUMBER "));
add (tfNum1);
add (new Label ("Message = "));
add (taMessage);
btnEnter = new Button ("Enter");
add (btnEnter);
btnEnter.addActionListener (new EnterHandler(this));
addWindowListener(new CloseWindow());
}
public void load_aNum()
{
aNum = new int[50];
aNum[0]++;
}
private static boolean isValid(int pInput)
{
boolean bResult;
bResult = (pInput >=0) && (pInput<=9999);
return bResult;
}
class EnterHandler implements ActionListener
{
khNumberDisplayer myFrame;
EnterHandler (khNumberDisplayer pFrame)
{
myFrame=pFrame;
}
public void actionPerformed (ActionEvent event)
{
myFrame.EnterStuff();
myFrame.repaint();
}
}
public void EnterStuff()
{
String sMessage;
boolean valid_test;
int num1;
num1 = Integer.parseInt(tfNum1.getText());
//valid_test = (num1 >=1 && num1 <=200);
if (isValid(num1))
{
aNum[aNum[0]] = num1;
int total;
total = aNum[aNum[0]] + aNum[aNum[0]-1];
sMessage = "Number is valid. " + total;
}
else
sMessage = "Number is not valid";
taMessage.setText(sMessage);
}
public void actionPerformed(ActionEvent event)
{
repaint();
}
public static void main (String []args)
{
Frame NumberDisplayerWindow = new khNumberDisplayer();
NumberDisplayerWindow.setSize (700,300);
NumberDisplayerWindow.setBackground(Color.pink);
NumberDisplayerWindow.show();
load_aNum();
}
public class CloseWindow extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}
}
--
-Kelly
kelly at farringtons dot net
Check out www.snittens.com