Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: 2DArry from Textbox input (pls hlp)

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 2DArry from Textbox input (pls hlp)

    This is for a school assignment. I am unable to populate my 2D array with user input from GUI textbox and then have it return values from the array in a text area. There are some calculations that need to be performed, but I haven't gotten that far because I cannot get this 2D array to work right. I am new at Java, so any help is apprieciated. Below is my code: (pls forgive the long variable names, like i said, i'm new @ this)
    /**
     * This application provides a GUI where the user enters the session
     * minutes and rates.
     */
     
    package gtt1t1;
    //import Java class
    import javax.swing.JFrame;
    /**
     *
     * @author MDH
     */
    public class Gtt1T1 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            //create new Gtt1T1Frame
            JFrame frame = new Gtt1T1Frame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
     
    }
    /*
     * This is the Frame class for Gtt1T1 package
     * all methods and classes included.
     */
    package gtt1t1;
    //import Java classes
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
     
    /**
     *
     * @author Monica Dorsey-Hull
     */
    public class Gtt1T1Frame extends JFrame
    {
        //frame variables
        static final int FRAME_WIDTH = 200;
        static final int FRAME_HEIGHT = 400;
        static final int AREA_ROWS = 10;
        static final int AREA_COLS = 12;
        static final int SESSION_MINS = 0;
        static final int SESSION_RATE = 0;
        public JLabel minsLabel;
        public JTextField minsField;
        public JLabel rateLabel;
        public JTextField rateField;
        public JTextArea reportArea;
        public JButton enterButton;
        public JButton reprtButton;
        public JButton quitButton;
        public JPanel panel;
        public static int minInt;
        public static int rateInt;
        public int index = 0;
        int[][] tArray = new int[index][2];
        public String txtArray;
     
     
        public Gtt1T1Frame()
        {
            //text area variables & parameters
            reportArea = new JTextArea(AREA_ROWS, AREA_COLS);
            reportArea.setEditable(false);
            //methods for the GUI to operate
            createTextField();
            createButton();
            createPanel();
            setSize(FRAME_WIDTH, FRAME_HEIGHT); //GUI size
        }
     
        private void createTextField()
        {
            minsLabel = new JLabel("Session Minutes"); //label for minutes
            int fwidth = 6; //label width for all text fields
            minsField = new JTextField(fwidth); //text field for minites
            minsField.setText("30"); //preset value of text field
            rateLabel = new JLabel("Session Rate    "); //label for payrate
            rateField = new JTextField(fwidth);
            rateField.setText("15"); //preset value of text field
        }
     
        private void createButton()
        {
            enterButton = new JButton("Enter"); //create Enter button
            class AddEnterListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    Scanner input = new Scanner(System.in);
                    //convert text input to integer for array
                    int minInt = Integer.parseInt(minsField.getText());
                    int rateInt = Integer.parseInt(rateField.getText());
     
                    //input array values in a loop
                    while (index > 0)
                    {
                        tArray[index][0] = input.nextInt(minInt);
                        tArray[index][1] = input.nextInt(rateInt);
                    }
                    index++;
                    //checks for incorrect values entered
                    if (minInt < 0) {
                        IllegalArgumentException exception
                                = new IllegalArgumentException("Invalid Value");
                        throw exception;
                    }
                    if (rateInt < 0) {
                        IllegalArgumentException exception
                                = new IllegalArgumentException("Invalid Value");
                        throw exception;
                    }
                    //resets the text fields for new values
                    minsField.setText("");
                    rateField.setText("");
     
                }
            }
            ActionListener eListener = new AddEnterListener();
            enterButton.addActionListener(eListener);
     
            quitButton = new JButton("Quit");
            class AddQuitListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    System.exit(0); //closes GUI
                }
            }
            ActionListener qListener = new AddQuitListener();
            quitButton.addActionListener(qListener);
     
            reprtButton = new JButton("Report");
            class AddReportListener implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    //prints array items in text area
                    for (int cols = 0; cols <= 2; cols++){
                    String txtArray = Integer.toString(tArray[index][cols]);
                    reportArea.append(txtArray + "\n");}
     
                }
            }
            ActionListener rListener = new AddReportListener();
            reprtButton.addActionListener(rListener);
        }
     
        private void createPanel()
        {
            panel = new JPanel();
            panel.add(minsLabel);
            panel.add(minsField);
            panel.add(rateLabel);
            panel.add(rateField);
            panel.add(enterButton);
            panel.add(quitButton);
            panel.add(reprtButton);
            JScrollPane scrollPane = new JScrollPane(reportArea);
            panel.add(scrollPane);
            add(panel);
        }
     
     
    }
    Last edited by helloworld922; December 31st, 2010 at 02:56 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 2DArry from Textbox input (pls hlp)

    As long as the variable names are meaningful, long names are good

    Could you describe what you mean by "2D arrays don't work right"? If you're getting any exceptions, please post the exception code in its entirety.

    Also, in the future please surround your code with highlight tags. I've modified this post accordingly this time.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    MoniD (January 1st, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: 2DArry from Textbox input (pls hlp)

    First off,

    int[][] tArray = new int[index][2];

    means

    int [][] tArray = new int[0][2];

    which means that there are 3 values in row 0 and no values in any other row.

    Normally you define the array itself inside the constructor.

    while (index > 0)
                    {
                        tArray[index][0] = input.nextInt(minInt);
                        tArray[index][1] = input.nextInt(rateInt);
                    }

    You're saying that this is true while index is greater than 0.

    However, any value for index greater than 0 is out of bounds.

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    MoniD (January 1st, 2011)

  6. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: 2DArry from Textbox input (pls hlp)

    ActionListener eListener = new AddEnterListener();
    AddActionListener is probably a void method.

    I very highly doubt it returns an ActionListener or a subclass of ActionListener.

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    MoniD (January 1st, 2011)

  8. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: 2DArry from Textbox input (pls hlp)

    Not quite 100% sure if this will work, but you could try changing
    //checks for incorrect values entered
                    if (minInt < 0) {
                        IllegalArgumentException exception
                                = new IllegalArgumentException("Invalid Value");
                        throw exception;
                    }
                    if (rateInt < 0) {
                        IllegalArgumentException exception
                                = new IllegalArgumentException("Invalid Value");
                        throw exception;
                    }

    to
    //checks for incorrect values entered
    try()
    {
    int minInt = Integer.parseInt(minsField.getText());
                    if (minInt < 0) {
                       throw  new IllegalArgumentException("Invalid Value");
     
                    }
    }
     
    catch (IllegalArgumentException iaeRef)
    {
    // probably make a JOptionPane like this:
    JOptionPane.showMessageDialog(null, "Invalid value", "IllegalArgumentException", JOptionPane.ERROR_MESSSAGE);
    }
     
    try()
    {
    int rateInt = Integer.parseInt(rateField.getText());
                    if (rateInt < 0) {
     
                                throw new IllegalArgumentException("Invalid Value");
     
                    }
    }
     
    catch(IllegalArgumentException iaeRef)
    {
    JOptionPane.showMessageDialog(null, "Invalid Value", "IllegalArgumentException", JOptionPane.ERROR_MESSAGE);
    }

  9. The Following User Says Thank You to javapenguin For This Useful Post:

    MoniD (January 1st, 2011)

  10. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: 2DArry from Textbox input (pls hlp)

    Thank you so very much Javapenguin! You are a saint. Happy New Year!

  11. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: 2DArry from Textbox input (pls hlp)

    I modified the while statement to a for statement:
    for (index = 0; index > 0; index++)
                    {
                        tArray[index][0] = input.nextInt(minInt);
                        tArray[index][1] = input.nextInt(rateInt);
                    }
    Now I am able to enter more than once and not get an error. Currently I am unable to display the results of the array when I click on the "report" button.
     
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
    at gtt1t1.Gtt1T1Frame$1AddReportListener.actionPerfor med(Gtt1T1Frame.java:126)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 5 seconds)


    I am currently working on the error to see if I can figure it out myself. Thanks again for the help, staring at code for hours and I was getting nowhere with my first problem.

Similar Threads

  1. Replies: 2
    Last Post: December 30th, 2010, 01:28 PM
  2. Getting input
    By BuhRock in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2010, 10:30 AM
  3. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  4. TextBox
    By tryingtoJava in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 10:05 AM
  5. make textbox in canvas
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: October 6th, 2009, 07:10 AM