473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Basic array question, need help

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
Jul 17 '05 #1
4 5952
KellyH wrote:
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.


Are you able to make use of the ArrayList object at all? It may be far
simpler to use that than an actual integer array. Just make sure you use
Integer.parseInt() to validate the input, and then add it to your
ArrayList object (which should, of course, be declared at the class level).

--
Chris Shepherd

Jul 17 '05 #2

"KellyH" <Ke***@whatever.com> wrote in message
news:UqoBb.484596$Tr4.1330037@attbi_s03...

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):
<SNIP CODE>


I didn't look at your code, but will give you some general pointers:

* Since the array size has a known, maximum size [50 ?]
create it at program startup / initialisation time e.g

// Note: This is an example - your code may be
// different
int array = new array[50];

You will note that such an array is automatically zero-filled; you
can use this value as an 'empty element' marker. If you need
to use zero as a valid value, then fill the array with an arbitrary
value [say -1 ?] to act in this role

* Each time the input routine successfully completes add the value
to the array by incrementing an index e.g.

...
if (nextElement < array.length)
{
array[nextElement] = inputValue;
++nextElement;
}
...

'nextElement' will always point to the next enpty array
element.

* If not all elements have been filled you can still traverse
the array [and know when to stop] by checking the
current element value for -1.

This allows you to display, or otherwise manipulate
array contents even if it is not full

* If you need to have some sort of 'reset' operation,
ensure all it does is refill the array with -1 values. You
don't have to recreate the array again

I hope this helps.

Anthony Borla
Jul 17 '05 #3
"KellyH" <Ke***@whatever.com> wrote in message news:<UqoBb.484596$Tr4.1330037@attbi_s03>...
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.


I assume by "load" you mean allocate and initialize. The correct place
to do it is in the constructor, since that's exactly what constructors
are for:

/** Creates a new instance of khNumberDisplayer */
public khNumberDisplayer()
{
// Allocate and initialize array
aNum = new int[50];
aNum[0]++;

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

The reason you can't access aNum from main is because main is a static
method, which means it belongs to the khNumberDisplayer class in
general, not any particular instance of it. Static methods cannot
access non-static member variables, since static methods can be
executed even when no instances of the class exist. If you really
wanted to allocate the array from main rather than the constructor
(though there's no good reason to), you could do it this way:

public static void main (String []args)
{
Frame NumberDisplayerWindow = new khNumberDisplayer();
NumberDisplayerWindow.setSize (700,300);
NumberDisplayerWindow.setBackground(Color.pink);
NumberDisplayerWindow.show();

// Call load_aNum on the instance
NumberDisplayerWindow.load_aNum();
}
Jul 17 '05 #4

Thanks everyone for your help! I didn't realize I could instantiate the
array in the constructor. Got it working now. Moving on to other
problems...
--
-Kelly
kelly at farringtons dot net
Check out www.snittens.com

"Karl von Laudermann" <ka**@ueidaq.com> wrote in message
news:e7**************************@posting.google.c om...
"KellyH" <Ke***@whatever.com> wrote in message news:<UqoBb.484596$Tr4.1330037@attbi_s03>...
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.


I assume by "load" you mean allocate and initialize. The correct place
to do it is in the constructor, since that's exactly what constructors
are for:

/** Creates a new instance of khNumberDisplayer */
public khNumberDisplayer()
{
// Allocate and initialize array
aNum = new int[50];
aNum[0]++;

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

The reason you can't access aNum from main is because main is a static
method, which means it belongs to the khNumberDisplayer class in
general, not any particular instance of it. Static methods cannot
access non-static member variables, since static methods can be
executed even when no instances of the class exist. If you really
wanted to allocate the array from main rather than the constructor
(though there's no good reason to), you could do it this way:

public static void main (String []args)
{
Frame NumberDisplayerWindow = new khNumberDisplayer();
NumberDisplayerWindow.setSize (700,300);
NumberDisplayerWindow.setBackground(Color.pink);
NumberDisplayerWindow.show();

// Call load_aNum on the instance
NumberDisplayerWindow.load_aNum();
}

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: solartimba | last post by:
I am a stats man that uses C++ to analyze stock data, but I have run into a problem regarding memory usage. First, though, I need to give a little background info. I want to write a program...
2
by: bbxrider | last post by:
i'm new to javascript but have programmed lots in vb, cobol, basic, etc jscript seems much more object oriented to me, much more direct manipulation of properties at least, than vb, don't know...
5
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
41
by: Psykarrd | last post by:
I am trying to declare a string variable as an array of char's. the code looks like this. char name; then when i try to use the variable it dosn't work, however i am not sure you can use it...
10
by: Jason Curl | last post by:
Greetings, I have an array of 32 values. This makes it extremely fast to access elements in this array based on an index provided by a separate enum. This array is defined of type "unsigned long...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
1
by: bruce | last post by:
hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
12
by: sheldonlg | last post by:
This is kind of basic, and I googled but didn't find much help. I have an array of text element fields. They all need to have the same name. So, let the name be either all with a or build...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.