GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Loop not performing as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop not performing as expected" Watch "Loop not performing as expected" New topic
Author

Loop not performing as expected

Margaret Norkett
Greenhorn

Joined: Feb 09, 2013
Posts: 7
I have to print out a 2D array to a jTextField. I'm using the following code, but I only get the first two items in the array printed and then those repeat a lot of times. Can someone advise me on what needs to be changed? Thank you!
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

Clue: look closely at your first append statement.
Margaret Norkett
Greenhorn

Joined: Feb 09, 2013
Posts: 7
Kathleen Angeles wrote:Clue: look closely at your first append statement.


I've changed the i to row and then it prints the same row 5 times, then the second row 5 times, etc.

I did get it to print the last rows by changing the definition of the array -- although I thought I understood how to declare an array, apparently I've misunderstood something.

double[][] tutorearnings=new double[10][2] -- was my original declaration -- a 10 row, 2 column array if I understand such things properly. Once I changed it to double[][] tutorearnings= new double[10][5] it then prints the last row. It still repeats everything, though, until it's hit the 10th iteration. Ideally, if I could find a way to dynamically declare the array once all user input was received, that would work much better, however, I have not found an elegant solution to that using an array and the task I must perform limits me to using this array.
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

Look again at the same line. More closely.

Clue: The error is not on the 'i'.
More Clue: The error is on these 2: [i][0] and [i][1]. Note that you are inside a for loop, 2 at that. Why did you hardcode 0 and 1 ([0] and [1])?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Welcome to the Ranch

What’s a 2D array? You have never seen one in Java because thy don’t exist. They might in C/C++ but not in Java.
What you have is an array of arrays.
Margaret Norkett
Greenhorn

Joined: Feb 09, 2013
Posts: 7
Kathleen Angeles wrote:Look again at the same line. More closely.

Clue: The error is not on the 'i'.
More Clue: The error is on these 2: [i][0] and [i][1]. Note that you are inside a for loop, 2 at that. Why did you hardcode 0 and 1 ([0] and [1])?


I will confess that this formula was in this forum on another thread. It worked for the poster or so they said, and works for me with the exception of getting too many iterations. BUT I have worked through every possible combination of these two variables in every position with no success.

I know when I printed to the console, I only had to show the tutoring[row][i] and it printed the entire array there, but printing into a jtextarea seems to be a whole different animal.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Printing in the console and inside the jtextarea are the same somehow.

The only difference, based on your need, is where you put the carriage return.

Where print and println is used for the console, you must add the '\n' yourself in the proper location, when appending the data into the jtextarea.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7894
    
  21

Margaret Norkett wrote:BUT I have worked through every possible combination of these two variables in every position with no success.

Then either those two variables are incorrect or you haven't gone through all combinations.

I know when I printed to the console, I only had to show the tutoring[row][i] and it printed the entire array there, but printing into a jtextarea seems to be a whole different animal.

No, you're using it differently, because I don't see tutoring[row][i] anywhere in your code.

Why don't you explain to us what you think tutoring[i][0] and tutoring[i][1] are in the loop you've written?
Furthermore: What is 'i'?

Winston
niraj mr
Greenhorn

Joined: Feb 10, 2013
Posts: 1
see this for more detail:
http://howjavado.blogspot.in/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Welcome to the Ranch

I am afraid that link is no help to the present thread.
Margaret Norkett
Greenhorn

Joined: Feb 09, 2013
Posts: 7
The first person answering my question alluded to the variables I used in the two loops as being where I needed to look for my solution.

I have the first loop using the variable row and the second loop using the variable i.

No, I didn't show in that snippet of code actually USING row in the print statement, because the statement I was using as my example only used the second variable in the print statement. I have, however, in other attempts to get this to print in my expected manner (i.e. ONE iteration of the entire array). I'm working through them all again to see if I can spot the error. I do see where I have an extra curly bracket that shouldn't be there...

Margaret Norkett
Greenhorn

Joined: Feb 09, 2013
Posts: 7
I have it working now.



The variable index is incremented as people enter data into the array which was set to a rather large size. Since we don't really want to print a bunch of zeros from the fields in the array that are not populated, I use the index variable to limit the items returned. I want to sincerely thank everyone for their help on this.
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

Note that although it works, your 2nd loop is odd as it is useless and the same as hardcoding 0 and 1.

Margaret Norkett
Greenhorn

Joined: Feb 09, 2013
Posts: 7
Kathleen Angeles wrote:Note that although it works, your 2nd loop is odd as it is useless and the same as hardcoding 0 and 1.



It's coded that way because this is a 2 column array and I don't need it to iterate over the column area more than that.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7894
    
  21

Margaret Norkett wrote:
Kathleen Angeles wrote:Note that although it works, your 2nd loop is odd as it is useless and the same as hardcoding 0 and 1.
It's coded that way because this is a 2 column array and I don't need it to iterate over the column area more than that.

Yes, but what if it isn't? Or what if you want to add a column later on? Doesn't it makes sense to write code that will work regardless of the number of columns? And if that simply isn't possible, wouldn't it have been better to simply have two 1-dimenional arrays:
double[] tutoringColumn1;
and
double[] tutoringColumn2;
?

Funnily enough, your very first post was on the right lines as far as dealing with any number of columns; but you seem to have got bogged down overthinking the problem.

Sometimes it's best to just StopCoding (←click) and think. In fact, almost always, IMO.

Not all code that works is good.

Winston
Margaret Norkett
Greenhorn

Joined: Feb 09, 2013
Posts: 7
Winston Gutkowski wrote:
Margaret Norkett wrote:
Kathleen Angeles wrote:Note that although it works, your 2nd loop is odd as it is useless and the same as hardcoding 0 and 1.
It's coded that way because this is a 2 column array and I don't need it to iterate over the column area more than that.

Yes, but what if it isn't? Or what if you want to add a column later on? Doesn't it makes sense to write code that will work regardless of the number of columns? And if that simply isn't possible, wouldn't it have been better to simply have two 1-dimenional arrays:
double[] tutoringColumn1;
and
double[] tutoringColumn2;
?

Funnily enough, your very first post was on the right lines as far as dealing with any number of columns; but you seem to have got bogged down overthinking the problem.

Sometimes it's best to just StopCoding (←click) and think. In fact, almost always, IMO.

Not all code that works is good.

Winston


Thanks Winston -- if this were anything other than a homework assignment you would be spot on with your advice. HOWEVER, the specifications of the rubric were quite detailed in that it HAD to be a 2D array that was iterated over. My instructor actually provided the code to print out the array (well the looping part of it) and even she admitted that it was UGLY coding, but it satisfies the grading rubric.

I do appreciate everyone's input here. You all are a great bunch. I have one more assignment for this class and I'm done. Thankfully it uses an ArrayList rather than an array. I think from my reading at least that will be easier to deal with....at least I'm keeping my fingers crossed that it is!

Margaret
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
It is unfortunate when you are told to use things like multiple arrays rather than creating objects to hold the data. Then you could use a single array.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7894
    
  21

Margaret Norkett wrote:I have it working now...

Well done. Just for future reference, you can make a lot things easier for yourself by breaking things up, viz:and with 'for-each' loops its even easier:HIH

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop not performing as expected