Results 1 to 2 of 2
  1. #1
    sevendaysky is offline Member
    Join Date
    Apr 2013
    Rep Power

    Default 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?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Rep Power

    Default 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.

Similar Threads

  1. Replies: 4
    Last Post: 03-09-2011, 05:54 AM
  2. 2 dimensional array
    By sehudson in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 11:56 PM
  3. Replies: 5
    Last Post: 11-26-2009, 07:01 PM
  4. Problem - input in two-dimensional array
    By PVL268 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 04:58 AM
  5. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 07:21 PM

Posting Permissions

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