Results 21 to 27 of 27
Thread: I need help with my 2D Array
- 03-13-2013, 02:07 AM #21
Re: I need help with my 2D Array
From post#17
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.
One way to preserve the variable's value is to define it as a class variable.
i is a POOR name for the variable.
The variable holds the next session row's value. Use a name that says that.
You have to stop the user from entering more than 5 values. The code could test the value of the index to the next row and not use it or change it if its value is past the end of the array. Or the code could disable the button at 5 sessions so the user can't press it again.java.lang.ArrayIndexOutOfBoundsException: 5If you don't understand my response, don't ignore it, ask a question.
- 03-13-2013, 01:15 PM #22
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: I need help with my 2D Array
i think your for loop is a problem please refer this link:Java: Arrays -- 2-dimensional
i hope this will be help to youRegards
Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475
- 03-13-2013, 02:15 PM #23
Member
- Join Date
- Mar 2013
- Posts
- 19
- Rep Power
- 0
Re: I need help with my 2D Array
tamilarasi, thanks for the input but I eliminated the first for loop because I couldn't use it to populate the array. The second loop is working fine now. My main problem now is populating the 2D array. I was given the suggestion of creating instance variables for each location in the array. I guess I'm not grasping the Listener method properly, which is why I'm researching on it now. I just don't understand how the listener method would know that after I press the button move to the next row in the array. Because this is what I can't get to happen.
- 03-13-2013, 03:33 PM #24
Re: I need help with my 2D Array
The nextSessionRow variable would have the value of the index to the row in the array to use. After the listener method uses that slot, it should increment the nextSessionRow variable so it points to the next empty slot.how the listener method would know that after I press the button move to the next row in the arrayIf you don't understand my response, don't ignore it, ask a question.
- 03-13-2013, 05:57 PM #25
Member
- Join Date
- Mar 2013
- Posts
- 19
- Rep Power
- 0
Re: I need help with my 2D Array
It's just not clicking with me. I don't mean to be a pest, I am just trying to understand. I have been doing Java for about 4 weeks now and I have been teaching myself online. It is just not incrementing. As a matter of fact, if I leave the code the way it is it reacts like a loop. I created instance variables for the rows, but couldn't see how to implement that in my actionPerformed method so I commented them out. Here is the code I currently have:
Java Code:public class TutorEarnings extends javax.swing.JFrame { //Instance Variable private double sessionMinutes; private double sessionEarnings; private double totalmin; private double totalearn; private double avgwage; private double minwage; private double areaMessage; double data[][] = new double[5][2]; //double rowValues = data[0][0]; //double row1Values = data[1][0]; //double row2Values = data[2][0]; //double row3Values = data[3][0]; //double row4Values = data[4][0]; private void btn1ActionPerformed(java.awt.event.ActionEvent evt) { /** collect each session time and session earnings, assign them to * sessionMinutes and sessionEarnings variable, then store each entry into the array * tutor, and finally clear the Text Fields */ try { String sessionT = tBox1.getText(); String wage = tBox2.getText(); double sessionMinutes = Double.parseDouble(sessionT); double sessionEarnings = Double.parseDouble(wage); tBox1.setText(""); tBox2.setText(""); data[0][0] = (double) sessionMinutes; data[0][1] = (double) sessionEarnings; data[1][0] = (double) sessionMinutes; data[1][1] = (double) sessionEarnings; data[2][0] = (double) sessionMinutes; data[2][1] = (double) sessionEarnings; data[3][0] = (double) sessionMinutes; data[3][1] = (double) sessionEarnings; data[4][0] = (double) sessionMinutes; data[4][1] = (double) sessionEarnings; } catch (Exception e) { e.printStackTrace(); System.out.println("Invalid input. Please try again."); } JOptionPane.showMessageDialog(null, "Session Time and Session Earning Entered."); }
- 03-13-2013, 06:33 PM #26
Re: I need help with my 2D Array
You need to consider how the user enters data and how that data gets put into the array. There is a sort of loop that goes from user to the listener and then the user again and again the listener, etc
Pseudo code:
set nSR = 0; // index to the array
begin loop to get session data from user
user enters data
user presses button
listener gets control
listener gets data user entered for one session
listener saves data in array using nSR as index
listener increments nSR
end loopIf you don't understand my response, don't ignore it, ask a question.
- 03-14-2013, 06:24 AM #27
Member
- Join Date
- Mar 2013
- Posts
- 19
- Rep Power
- 0
Re: I need help with my 2D Array
I totally GOT IT! Thanks. I finally figured out about the variable representing the row location in the array. After I got that straight, I was able to see how the listener incriminated through the array. Again, thanks Norm.
Java Code:public class TutorEarnings extends javax.swing.JFrame{ //Instance Variable private double sessionMinutes; private double sessionEarnings; private double totalmin; private double totalearn; private double avgwage; private double minwage; private double areaMessage; double data[][] = new double[5][2]; int sessionRows = 0; String sessionT = tBox1.getText(); String wage = tBox2.getText(); double sessionMinutes = Double.parseDouble(sessionT); double sessionEarnings = Double.parseDouble(wage); //Store the variable values in the data array data[sessionRows][0] = sessionMinutes; data[sessionRows][1] = sessionEarnings; sessionRows++; //clear JTextFields tBox1.setText(""); tBox2.setText("");
Similar Threads
-
[Problem] Enhanced for-loop with 2D arrays (or array in array)
By thewrongsyntax in forum New To JavaReplies: 0Last Post: 10-07-2012, 08:49 PM -
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks