home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

java help! cant access array, srry for size

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1575
Discussions: 200,955, Posts: 2,379,493, Members: 246,327
Old December 5th, 2003, 04:58 PM   Digg it!   #1 (permalink)
Senior Member
 
The Terk's Avatar
 
Join Date: Jul 2002
Location: Texas A&M
Posts: 611
Send a message via AIM to The Terk
java help! cant access array, srry for size

i have a problem with java: the problem is the user is prompted to insert the values of 10 integers and they are stored in an array, then an applet is used for the user to put in an index and show the value...

ok, i am havin a problem passing an array to this funcion, it seems like whenever i want to put in the location, and hit show, nothing is being displayed... which is odd... thanks, i will answer all questions towards this question
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class ArrayDisplay extends JApplet implements ActionListener
{
private JTextField arrayValue, arrayElement;
private JButton jbtShow, jbtSave, jbtLoad;
private int array[];

public void init()
{
int array[] = getArray();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2,2));
p1.add(new JLabel("Array Index"));
p1.add(arrayValue = new JTextField(4));
p1.add(new JLabel("Array Element"));
p1.add(arrayElement = new JTextField(4));
arrayValue.setEditable(true);
arrayElement.setEditable(true);
p1.add(jbtShow = new JButton("Show Elements"));
p1.add(jbtSave = new JButton("Save Elements"));
p1.add(jbtLoad = new JButton("Load Elements"));
p1.setBorder(new TitledBorder("Display Array"));

jbtShow.addActionListener(this);
jbtSave.addActionListener(this);
jbtLoad.addActionListener(this);

this.getContentPane().add(p1,BorderLayout.NORTH);
}

public static int[] getArray() // creates the user defined array
{
int array[] = new int[10];
// Read all numbers
for (int i=0; i<array.length;i++)
{
//try
{
String numString = JOptionPane.showInputDialog(null, "Enter a number:","The Array Counter", JOptionPane.QUESTION_MESSAGE);
//Convert string into integer
array[i]=Integer.parseInt(numString);
}

/* catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"That value is too large");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"That value is too small");
}*/
}
return array;
}


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbtShow)
{
String tempString;
int a;
tempString = arrayValue.getText().trim(); //take the field and trim it
a=(new Integer(tempString)).intValue(); //convert it to a int
int indexValue = array[a];
arrayElement.setText(String.valueOf(indexValue));
}

}
}
__________________

The Terk
The Terk is offline   Reply With Quote
Old February 14th, 2009, 03:19 AM     #2 (permalink)
Junior Member
 
Join Date: Feb 2009
Posts: 1
Red face
dont know but i have the same problem

i am trying to get something like this to work....

class filling{
filling(){}
void put_it_in(){//put code here};
}

class taco {

public filling inside[];

taco(){

filling meat;
filling lettuce;
filling sour_cream;

inside[] = {meat, lettuce, sour_cream};

}
}//gives me a missing ; error
ericgl is offline   Reply With Quote
Old February 17th, 2009, 06:44 PM     #3 (permalink)
Ultimate Member
 
dchw_dude's Avatar
 
Join Date: Nov 2004
Location: Provo, UT
Posts: 1,337
@ The Terk - You look like you have your listeners set up correctly. Maybe write a small loop that will attempt to read the array in one of your listeners. Have it dump its output to the console for debugging. If you start getting null pointer exceptions, something is wrong when you hand the array to your listener function here.

And when you say integer, do you mean the int primitive type or objects of class "Integer"? integer objects have some different properties than int primitive types.

@ericgl - first, the filling method is a constructor right? It should read as "public filling(){...(code here)...} You also do not need a semicolon after the putitin method unless filling is an abstract class and putitin is an abstract method of that class.

Nex time, for my own readability, pretty please use the tags.

Happy hacking!



EDIT - And wow. I just realized that this is an incredibly old thread thats been dug up by someone else. Please disregard my post.
__________________
Team TechIMO - Team # 111
http://folding.stanford.edu/

Last edited by dchw_dude : February 17th, 2009 at 06:55 PM.
dchw_dude is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2947)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
Regular Build (11)
windows 7 problem (7)
Laptop with wireless problem. (5)
Is the PSU I received dead? (12)
Print spooler problem (15)
windows vista security holes (11)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Recent Discussions
Open With ..... Win7 (1)
Kingston Bluetooth Dongle Driver (0)
webcam (0)
upgrade for hp a6101 (0)
windows vista security holes (11)
Laptop with wireless problem. (5)
Modern Warfare 2: Who Bought It? (64)
tv not turn on-makes clicking sound (2)
CPU fan stops spinning randomly (9)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)


All times are GMT -4. The time now is 06:57 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28