Results 1 to 2 of 2
Thread: 2-Dimensional Array and input
- 04-05-2013, 09:13 PM #1Member
- Join Date
- Apr 2013
- Rep Power
2-Dimensional Array and input
Okay. I'm taking an Intro to Programming I class. One of the assignments for completion involves building an app (as you'd expect). I have a fair understanding of the overall goal and how to get there, but I keep tripping up on the input-and-array portion. ArrayList seems like it would work better for this purpose but the instructions explicitly state to create and use a 2-dimensional array. I can figure out how to initialize a 2-D array, but it's the input part that I get hung up on. I've done the basic "take input, print input" but I'm totally blanking on how to get a GUI button to take input from 2 text fields (integers) and store them in this array. After that I have to perform a number of functions against this information (totals and averaging) but that part I think I can handle.
What's the process for taking input from 2 fields (via a button click) and storing into an array?
- 04-05-2013, 11:07 PM #2
Re: 2-Dimensional Array and input
Assuming that you are referring to a JTextField, there is a method called getText() that will return a String whose value is the contents of that JTextField.
Approach it step by step:
1)Retrieve the value from the JTextField, and store it in a variable
String userInput = myJTextField.getText();
2)Repeat the process for your 2nd JTextField, and store it's value in another variable.
3) Storing those 2 values in a 2D Array is simply a matter of knowing which array you want to store the value in, and at what index in that array you want to store it in. Just be sure that you have initialized the Array beforehand.
- By ziongio in forum New To JavaReplies: 4Last Post: 03-09-2011, 06:54 AM
- By sehudson in forum New To JavaReplies: 5Last Post: 02-21-2011, 12:56 AM
- By Peanuts1 in forum New To JavaReplies: 5Last Post: 11-26-2009, 08:01 PM
- By PVL268 in forum New To JavaReplies: 5Last Post: 03-09-2009, 05:58 AM
- By kHim in forum New To JavaReplies: 4Last Post: 11-16-2008, 08:21 PM