»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2003, 01:58 PM   #1 (permalink)
Registered User
 
The Terk's Avatar
 
Join Date: Jul 2002
Location: Texas A&M
Posts: 438
The Terk is on a distinguished road
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
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 10:21 AM.