How to store user input in an array element?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How to store user input in an array element?

  1. #1
    Join Date
    Nov 2004
    Posts
    3

    How to store user input in an array element?

    I'm new to Java and have been working on an assignment for days w/ no luck.

    I'm happy to post my program if needed BUT for now just looking for a hint(s) and clarification.

    I am trying to write a 'simple' applet that will use a one dimensional array w/ 5 elements. Basically I want my user to enter a number in the text field, I then want to validate the number- must be between 10-100, I want to validate the entriescounter which must be less than 5, then (finally) I want to search my array to see if the users number was previously entered, if not I want to append that number into the array (how?) if the number is alrady stored I want to increment my counter but ignore the input. I want the unique user input to dispay in another jtextfield and I want my error responses to display in the showStatus box- ie, invalid entry after the valid test and over 5 entries made- if entries counter reaches 5.

    I am new to this site as well- so if I have asked too much I'm sorry in advance.

    Thanks- any help on arrays and appending input from a user is most appreaciated.

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    ok i understand your question, but where are you at? Do you have any problems with any specific areas?

    Have you done much with GUI stuff before?
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Nov 2004
    Posts
    3
    Kram thanks for the reply - I'm pretty lost at this point. I sorta get the basic GUI elements. My program logic is lacking- I don't have my validation set up in the right place(s) I guess and my error responses are suppose to display next to my counter in the showStatus area- yet I couldn't get that right so I put them in another JText field - ug! I seriously don't get how the appending/storing user input to my array element works. I keep thinking about applications and using strings to parse Int the input- I don't know what the equiv is in an applet. (does any of that even make sense?)


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class A7_13 extends JApplet implements ActionListener {

    JLabel enterLabel, resultLabel;
    JTextField enterField, resultField;

    int array[];

    // set up applet's GUI
    public void init()
    {
    // get content pane and set its layout to FlowLayout
    Container container = getContentPane();
    container.setLayout( new FlowLayout() );

    // set up JLabel and JTextField for user input
    enterLabel = new JLabel( "Enter number:" );
    container.add( enterLabel );

    enterField = new JTextField( 4 );
    container.add( enterField );

    // register this applet as enterField's action listener
    enterField.addActionListener( this );

    // set up JLabel and JTextField for displaying results
    resultLabel = new JLabel( "Numbers entered:" );
    container.add( resultLabel );

    resultField = new JTextField( 10 );
    resultField.setEditable( false );
    container.add( resultField );

    // I added this thinking that I need it later
    // to validate the numbers entered it is useless here
    int value = 0;
    int number = 0;

    // create array that will be populated by users input
    array = new int [ 5 ];

    for ( int counter = 0; counter < array.length; counter++ )
    array[ counter++ ] = value;
    // I think the above is the right format
    // I don't have the variable value set up
    //I want that to somehow append the input to
    //the array elements- validate, search, display

    if ( number < 10 || number > 100 )
    showStatus( "Invalid Entry!" );
    return; // i think this is right logic but
    //do I need more if statements for my counter
    // like if counter < 5
    //return showstatus "over 5 entries"

    } // end method init



    // search array for specified key value
    public int linearSearch( int array2[], int key )
    {
    // loop through array elements
    for ( int counter = 0; counter < array2.length; counter++ )


    // if array element equals key value, return location
    if ( array2[ counter ] == key )
    return counter;

    return -1; // key not found
    // this is suppose to be my search piece

    } // end method linearSearch

    // obtain user input and call method linearSearch
    public void actionPerformed( ActionEvent actionEvent )
    {
    // input also can be obtained with enterField.getText()
    String searchKey = actionEvent.getActionCommand();


    // pass array reference to linearSearch; normally, a reference to an
    // array is passed to a method to search corresponding array object
    int element = linearSearch( array, Integer.parseInt( searchKey ) ); // this is my search should I append here


    // display search result
    if ( element != -1 )
    resultField.setText( "number is " + element );
    else
    showStatus( "Invalid entry" );

    showStatus( "Numbers entered " + value );

    enterField.setText( "" ); //clear field


    } // end method actionPerformed

    } // end class A7_13

  4. #4
    Join Date
    Nov 2004
    Posts
    3
    I think I figured out part of my problem- my if validation should not be in the INIT- I am going to try moving it into the actionPerformed.

  5. #5
    Join Date
    Sep 2004
    Posts
    223
    yeah thats a good place to do it, as you will probably have figured out, when ever a button or field get activated by the user the actionPerformed() method get fired with an ActionEvent passed to it, this even can be used to access what exactly has been pushed or edited. Then once you know that the user has pushed the "Go" button, you can code in the parts that gather the text from the fields and put them into variables for validation
    A kram a day keeps the doctor......guessing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center