Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   My loop only calculates odd numbers, not even, please help. (https://www.askmehelpdesk.com/showthread.php?t=818340)

  • Nov 16, 2015, 07:59 AM
    CThoms
    My loop only calculates odd numbers, not even, please help.
    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);

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

    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.
  • Nov 17, 2015, 08:06 AM
    CThoms
    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.
  • Nov 17, 2015, 08:33 AM
    CravenMorhead
    Quote:

    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?
  • Nov 18, 2015, 07:48 AM
    CThoms
    Quote:

    Originally Posted by CravenMorhead View Post
    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.
  • Nov 18, 2015, 08:03 AM
    CThoms
    Quote:

    Originally Posted by CravenMorhead View Post
    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
  • Nov 18, 2015, 09:03 AM
    CravenMorhead
    That is for a simple reason.
    Quote:

    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 for more information
    Good luck.
  • Nov 19, 2015, 08:13 AM
    CThoms
    Quote:

    Originally Posted by CravenMorhead View Post
    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 for more information
    Good luck.

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

  • All times are GMT -7. The time now is 10:55 PM.