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