PDA

View Full Version : My loop only calculates odd numbers, not even, please help.


CThoms
Nov 16, 2015, 07:59 AM
while ( x < 20 ) {

if ( r == 11 ) {

r = 0;
texts = texts + "\n";
textArea.setText(texts);
texts = texts + "\n";
textArea.setText(texts);

x++;

}

if ( s == 11 ) {

s = 0;
texts = texts + "\n";
textArea.setText(texts);
texts = texts + "\n";
textArea.setText(texts);

x++;

}

texts = texts + columnNames[r] + " : " + firstevolutiondata[x][s] + " \n";
s++; r++;

textArea.setText(texts);

System.out.println(x);

}

CravenMorhead
Nov 16, 2015, 09:19 AM
Could it be that you're incrementing X twice before you get to the bottom?


while (x < 20)

if(r == 11)
{
do stuff
x++;
}

if(s==11)
{
do stuff
x++;
}
do stuff;
s++;r++;
do more stuff
}


It seems that you're populating a 11x20 array. The way you're doing it, you're skipping over an column. I would reduce s,r to one value, call it n. When n is eleven you do the stuff, reset n to zero, do the stuff, then increment x. That should solve the problem.

CThoms
Nov 17, 2015, 08:06 AM
Thanks for your help, I did what you said, but, it says that this line is the error :texts = texts + columnNames[n] + " : " + firstevolutiondata[x][n] + " \n";And I only get 0's, 1's, 2's and, 3's I'm utterly confused.

CravenMorhead
Nov 17, 2015, 08:33 AM
int n=0;
int x=0;
while ( x < 20 ) {

if ( n == 11 ) {

n = 0;
texts = texts + "\n";
textArea.setText(texts);
texts = texts + "\n";
textArea.setText(texts);

texts = texts + "\n";
textArea.setText(texts);
texts = texts + "\n";
textArea.setText(texts);

x++;

}

texts = texts + columnNames[n] + " : " + firstevolutiondata[x][n] + " \n";
n++;

textArea.setText(texts);

System.out.println(x);

}

That is the simple simplification. I don't know what the values of r and s were coming into the original code. What is this meant to do? What is the parameters of your assignment?

CThoms
Nov 18, 2015, 07:48 AM
That is the simple simplification. I don't know what the values of r and s were coming into the original code. What is this meant to do? What is the parameters of your assignment?

R & S were sort of the same thing, so, they're not really relevant anymore.

CThoms
Nov 18, 2015, 08:03 AM
That is the simple simplification. I don't know what the values of r and s were coming into the original code. What is this meant to do? What is the parameters of your assignment?
Here's the code as is now, I'm having troubles with it because it only prints the last result, which is " Froakie "
Program Example - Pastebin.com (http://pastebin.com/rLG64hvk)

CravenMorhead
Nov 18, 2015, 09:03 AM
That is for a simple reason.


texts = texts + "\n";


textArea.setText(texts);

texts = texts + "\n";
textArea.setText(texts);


Each time this is called it will overwrite the contents of the textArea. You don't see because it is happening too fast. If you put that "System.out.println(text)" at the bottom of the printstats function then you will see all the stats printed out.

Also this is a lesson in scope. You passing the string by value, the contents of it, to the print stat function. You get the contents of that string in your function but not the actual string value. All the changes you make to it will stay in the function. When you leave the function the copy of the string will be destroyed and WON'T be carried on. Each time you go into the printstats function you have an empty string. If you change the way you allocate the string before you pass it in, you should be able to collect all the stats.
string texts = new string("");
instead of
string texts = "";

you might get all the stats. It is hard to say.
Look at this (http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) for more information
Good luck.

CThoms
Nov 19, 2015, 08:13 AM
That is for a simple reason.


Each time this is called it will overwrite the contents of the textArea. You don't see because it is happening too fast. If you put that "System.out.println(text)" at the bottom of the printstats function then you will see all the stats printed out.

Also this is a lesson in scope. You passing the string by value, the contents of it, to the print stat function. You get the contents of that string in your function but not the actual string value. All the changes you make to it will stay in the function. When you leave the function the copy of the string will be destroyed and WON'T be carried on. Each time you go into the printstats function you have an empty string. If you change the way you allocate the string before you pass it in, you should be able to collect all the stats.
string texts = new string("");
instead of
string texts = "";

you might get all the stats. It is hard to say.
Look at this (http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) for more information
Good luck.

Thanks for all of your help, I finally figured it out, thanks to you! :D