• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array help  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I completely new to programming and having quite a few issues getting my first Java program completed. I am trying to pass 2 arguments from a swing GUI and send them to a two dimensional array. I have looked around but I can't seem to find any examples of how this is supposed to work. It seems I have no trouble manually filling elements in an array, but just cannot pass data into them. Am I just completely missing something here?

Here is the method I am working with:


and here is the button:

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things:

1. Why are you looping and setting the same values each time through the loop (lines 16-18, first listing)? The value of ticker is changing but the values you're assigning aren't. Is that what you really want to do?
2. What do you see getting displayed by line 21 of the first listing? Some kind of random cryptic string, right? Check this page out: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
3. Also, how many times does line 21 of the first listing get executed? Is that what you expected/wanted to happen?
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

To go into pedantic mode, you haven’t got a 2D array because there ain’t no such thing. You have an array of arrays, which is actually better.
It is also probably not good design to put different values in an array like that. I would suggest you need a WorkTime object with start and finish times, and you create a WorkTime[] array. Only you can probably think of a better name for the class than I did.
 
Zack Denali
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, glad there are people that are out here willing to help!

I am really confused about your first statement. I noticed you have posted it on other threads having multidimensional array questions such as myself. I am working from "Java A Beginners Guide" fifth edition and when I reference it for creating an array it says the simplest form of a multidimensional array is the two dimensional array. My coursework also refers to me creating a two dimensional array for pay and time. I have read the nuts and bolts of arrays and keep referring to my textbook but I am not grasping the "array of arrays" terminology. In my head I see timepay = new double [2] [15] creating a spreadsheet with 2 rows and 15 columns is this not what is happening?

Campbell Ritchie wrote:Welcome to the Ranch

To go into pedantic mode, you haven’t got a 2D array because there ain’t no such thing. You have an array of arrays, which is actually better.
It is also probably not good design to put different values in an array like that. I would suggest you need a WorkTime object with start and finish times, and you create a WorkTime[] array. Only you can probably think of a better name for the class than I did.
 
Zack Denali
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu, thanks for your help.

1. I was trying to use a for loop to change which element the argument passed into setTimePay would be placed into. Is there a better way to cycle through an array with user input?
2. I do get some gibberish, I have edited the code quite a bit since last night and I can get it to display numbers now but only in the first elements of the array. It still will not cycle through the elements like I wanted it to.
3. That line doesn't work as I had planned either, I found a better way to read out an array on the forums last night.

My tasks don't seem like they should be that hard....take user input, store values in a multidimensional array(still unsure of the terminology here) use the data contained in the elements to make computations and display the results to the user. I don't understand why this is difficult to me.

Junilu Lacar wrote:A few things:

1. Why are you looping and setting the same values each time through the loop (lines 16-18, first listing)? The value of ticker is changing but the values you're assigning aren't. Is that what you really want to do?
2. What do you see getting displayed by line 21 of the first listing? Some kind of random cryptic string, right? Check this page out: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
3. Also, how many times does line 21 of the first listing get executed? Is that what you expected/wanted to happen?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!