|
-
Problem with Array
I am writing a program with arrays, the user is able to enter up to ten entries. The numbers get placed in the array, next the user is able to either search for a number in the array, or they can simply type in the index number and if its valid it would be displayed in a text field. I am able to do all of that, but all the numbers at index six and above are zero.
Code:
// ArrayAccess.java
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class ArrayAccess extends JFrame {
private JTextField inputField, retrieveValueField, retrieveIndexField, outputField;
private JPanel inputArea, inputPanel, retrievePanel, outputPanel;
private StringTokenizer splitter;
private int num, accessArray[];
private String input;
// set up GUI
public ArrayAccess()
{
super( "Accessing Array values" );
accessArray = new int[ 10 ];
// get content pane and set its layout
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// set up input Panel
inputPanel = new JPanel();
inputPanel.add( new JLabel( "Enter array elements here" ) );
inputField = new JTextField( 10 );
inputField.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
try
{
input = inputField.getText();
splitter = new StringTokenizer(input,",");
for(int x = 0; x <= splitter.countTokens(); x++)
{
accessArray[x] = Integer.parseInt(splitter.nextToken());
}
}
catch(Exception exc){System.out.println("Error");}
inputField.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
inputPanel.add( inputField );
container.add( inputPanel );
// set up retrieve Panel
retrievePanel = new JPanel( new GridLayout( 2, 2 ) );
retrievePanel.add( new JLabel( "Enter number to retrieve" ) );
retrieveValueField = new JTextField( 10 );
retrieveValueField.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
try
{
num = Integer.parseInt(retrieveValueField.getText());
for(int x = 0; x<accessArray.length;x++)
{
if(accessArray[x] == num)
{
outputField.setText("Number "+ num + " found at index: "+ x);
}
}
}
catch(NumberFormatException nfe){System.err.println("Bad Number Format");}
catch(NumberNotFoundException nnfe){System.err.println("Number Not Found");}
retrieveValueField.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
retrievePanel.add( retrieveValueField );
retrievePanel.add( new JLabel( "Enter index to retrieve" ) );
retrieveIndexField = new JTextField( 10 );
retrieveIndexField.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
try
{
num = Integer.parseInt(retrieveIndexField.getText());
outputField.setText("Number at index: " + num + " is "+ accessArray[num]);
}
catch(ArrayIndexOutOfBoundsException bounds){System.err.println("Bad index specified");}
catch(NumberFormatException number){System.err.println("Bad input");}
retrieveIndexField.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
retrievePanel.add( retrieveIndexField );
container.add( retrievePanel );
// set up output Panel
outputPanel = new JPanel();
outputPanel.add( new JLabel( "Result" ) );
outputField = new JTextField( 30 );
outputField.setEditable( false );
outputPanel.add( outputField );
container.add( outputPanel );
setSize( 400, 200 );
setVisible( true );
} // end constructor
// execute application
public static void main( String args[] )
{
ArrayAccess application = new ArrayAccess();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class ArrayAccess
Thanks for any suggestions
Similar Threads
-
Replies: 0
Last Post: 10-30-2002, 04:40 AM
-
Replies: 3
Last Post: 10-15-2002, 02:58 PM
-
By Adam Right in forum ASP.NET
Replies: 0
Last Post: 01-27-2002, 05:44 PM
-
By Jack in forum VB Classic
Replies: 2
Last Post: 09-02-2000, 07:04 PM
-
By Mike in forum VB Classic
Replies: 32
Last Post: 04-12-2000, 07:52 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks