Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Having problems with datatypes and 2D Array

    I am new to java and I am trying to grasp the whole 2D array concept. I declared an array for double datatype elements. I have two JTextFields that I am allowing users to input data. I want to store that data into a 2D array. From my understanding, JTextField produces a String. Well I need for that String to be stored as a double. My for each loop requires an int, which I also want to use double values. I can't seem to figure this out. Any help would be highly appreciated. Here's some of my code:

    Java Code:
    public class TutorEarnings3 implements ActionListener {
        //Instance Variables
        JButton btn1;
        JButton btn2;
        JButton btn3;
        JButton btn4;
        JTextField tBox1;
        JTextField tBox2;
        JTextArea tArea;
        double data[][] = new double[5][2];
    
    
    
    
    
    public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource() == btn1){
                 
                String sessionT = tBox1.getText();
                String wage = tBox2.getText();
                
                double session = Double.parseDouble(sessionT);
                double earnings = Double.parseDouble(wage); 
                
                tBox1.setText("");
                tBox2.setText("");
                //originField.setText("");
                //destinationField.setText("");
                 
                for(double i = 0; i < data.length; i++){
                    data[i] = (double)session;
                    for(double j = 0; j < data[i].length; j++){
                    data[j] = (double)earnings;
                    }
                }
    Last edited by dalythe; 03-12-2013 at 02:06 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    The for loop control variable should be an int. It counts looping and is used as an array index, both of which should be integers.

    To index into a 2 dim array for its slots you need to use two indexes: the2DimArray[index1][index2]

    The posted code would put the same value into all the slots in the array. It gets the value outside of the loop and then stores it into all the slots.
    What do you want in each slot of the array? Can you give an eaxmple?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    What I'm trying to do is input numbers with decimal points from two JTextFields and store one in the outer loop and the second in the inner loop. From my understanding, I need those values to have double datatypes for that needed precision. So I'm thinking I would need doubles in the array.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,529
    Rep Power
    5

    Default Re: I need help with my 2D Array

    Yes, but Norm was talking about the indices to the array elements. Not the elements themselves. The indices should be ints.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    store one in the outer loop and the second in the inner loop
    Can you give an example of where in the two dim array you want the data stored? Show what you want the array's contents to be.

    In terms of rows and columns, what data goes on a row and what data goes in each column on that row.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    I'm setting my array up to have 5 rows and 2 columns. The first column will represent time in 00.0 format and the second column will represent money in 00.00 format. For example, one row would contain 5.0 (hours) and 10.50 ($10.50)... I tried initially giving the indices variables i and j an int datatype and it didn't like that neither. That is why I am confused.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,529
    Rep Power
    5

    Default Re: I need help with my 2D Array

    You can't do this. data is a 2D array. You are trying to use it as two, one dimensional arrays.

    Java Code:
    for(double i = 0; i < data.length; i++) { 
         data[i] = (double)session;
          for(double j = 0; j < data[i].length; j++){
                data[j] = (double)earnings;
          }
    }
    Assuming I understand what you are trying to do, try something like this instead.

    Java Code:
    for(int i = 0; i < data.length; i++) { 
          data[i][0] = (double)session;
          data[i][1] = (double)earnings;
    }
    Note that this assigns the same values to each row.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    You haven't explained what data will go in the rows and columns. Your described its format.
    You did say that the first column was hours and the second column was dollars.
    What are the rows for?

    it didn't like that
    If you get error mesages, you need to copy the full text of the error messages and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    I'm not sure I understand what you are asking Norm. Each row will contain a time and a dollar amount. The time and amount will be inputted by the user through the JTextField. There will be five different sessions, so each row will represent each session time and dollar amount.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    Yes, that was what I was asking: a row has the data for a session

    How does the program get the data for one session and then for the next sessions?
    Does the user press a button when the data for the next session is ready?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    Yes. After entering the data for the session, the user press the enter button and the data is stored in the array. Then they enter the input for the next session and press enter again.

  12. #12
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    The following method will grab data and store in my array if I input whole numbers but I will have to be able to input whole numbers with decimals. jim829 after trying what you suggested, my array fills with duplicate values and that is not what I am trying to do. I'm looking to input one session value and store it then press enter to input more and store that in the same array. The code after my first for each loop is just for me to see if something was saved and what was saved. Here is my entire method:
    Java Code:
    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
            /** collect each session time and session earnings, assign them to
             * sessionT and wage variable, converts the strings to int, then store each entry into the array
             * tutor, and finally clear the Text Fields
             */
           String sessionT = tBox1.getText();
           String wage = tBox2.getText();
    
           int session = Integer.parseInt(sessionT);
           int earnings = Integer.parseInt(wage);
            
    
            tBox1.setText("");
            tBox2.setText("");
    
            for(int i = 0; i < data.length; i++) {
                data[i][0] = (double)session;
                data[i][1] = (double)earnings;
            }
            
    
                JOptionPane.showMessageDialog(null, "Session Time and Session Earning Entered.");
    
            for (int i =0; i < data.length; i++) {
                 for (int j = 0; j < data[i].length; j++) {
                 System.out.print(" " + data[i][j]);
                 }
                 System.out.println("");
            }
        }
    Last edited by dalythe; 03-12-2013 at 09:07 PM.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    After entering the data for the session, the user press the enter button and the data is stored in the array
    That says that ONE row of data is entered every time the user presses Enter. If only one row of data is entered for each call to the button's listener method, there is no need for a loop. You need to keep track of which row the current session's data should be stored in.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    By incrementing, shouldn't it automatically move to the next row? That is what I am trying to accomplish. Or should I be appending to the array? This is the output that is printed to the console after I enter 6 and 5 then press enter and then 10 and 11:

    6.0 5.0
    6.0 5.0
    6.0 5.0
    6.0 5.0
    6.0 5.0
    10.0 11.0
    10.0 11.0
    10.0 11.0
    10.0 11.0
    10.0 11.0

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    By incrementing, shouldn't it automatically move to the next row?
    What is being incremented? Shouldn't the array's row index be incremented by ONE every time a new row is saved?

    Does the program save the data in the array the way you want it to? It looks like all the rows of the array have the same data.

    appending to the array?
    You don't append to an array. arrays are fixed size.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    No it is not saving it properly. That is why I thought I should use a loop to move to the next row in the array, but it is not doing that. Currently the way it is, it just repeats the values in each row.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    You need a variable that holds the value of the next row in the array where the next session's data is to be stored.
    The variable needs to be defined where it's value will be preserved between calls to the listener method. The listener method will use it to access the row in the array and increment it to the next row for the next call to the listener method.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    No it is not saving it properly. That is why I thought I should use a loop to move to the next row in the array, but it is not doing that.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: I need help with my 2D Array

    Think about when the loop goes from one row of the array to the next.
    Compare that to when the user enters some data and presses a button.

    The loop will always go through ALL the rows every time it is executed.
    Re-read post#17
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    I hear what you are saying but I am trying to picture it. I don't understand how the program will know on this action event take these values and place them here, the next time action happens again place it here, then there. It seems as if something else would have to go there. I tried this code below and got the Exception at the bottom
    Java Code:
    int i = 0;
    data[i][0] = (double)session;
    data[i][1] = (double)earnings;
    i++;
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5
    at TutorEarnings.btn1ActionPerformed(TutorEarnings.ja va:280)
    at TutorEarnings.access$000(TutorEarnings.java:12)
    at TutorEarnings$1.actionPerformed(TutorEarnings.java :138)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 699)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    6.0 0.0 0.0 0.0 0.0Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5
    at TutorEarnings.btn1ActionPerformed(TutorEarnings.ja va:280)
    at TutorEarnings.access$000(TutorEarnings.java:12)
    at TutorEarnings$1.actionPerformed(TutorEarnings.java :138)
    1.0 0.0 0.0 0.0 0.0 at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 699)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Last edited by dalythe; 03-13-2013 at 02:05 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •