Ask Experts Questions for FREE Help !
Ask
    CThoms's Avatar
    CThoms Posts: 5, Reputation: 1
    New Member
     
    #1

    Nov 16, 2015, 07:59 AM
    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);

    }
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #2

    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's Avatar
    CThoms Posts: 5, Reputation: 1
    New Member
     
    #3

    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's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #4

    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's Avatar
    CThoms Posts: 5, Reputation: 1
    New Member
     
    #5

    Nov 18, 2015, 07:48 AM
    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.
    CThoms's Avatar
    CThoms Posts: 5, Reputation: 1
    New Member
     
    #6

    Nov 18, 2015, 08:03 AM
    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
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #7

    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 for more information
    Good luck.
    CThoms's Avatar
    CThoms Posts: 5, Reputation: 1
    New Member
     
    #8

    Nov 19, 2015, 08:13 AM
    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

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

How many odd three digit numbers can be formed from the 0,1,2,3,4 with repetition [ 2 Answers ]

How many odd three digit numbers can be formed from the 0,1,2,3,4 if digits can be repeated but first digit cannot be zero

What is the sum of the first 55 consecutive odd numbers? [ 1 Answers ]

What is the sum of the first 55 consecutive odd numbers?

Lcm only calculates two whole numbers, why [ 1 Answers ]

Why TI-84 Plus ONLY calculates LCM for two rational numbers

Confused how my bank calculates the montly installment. [ 2 Answers ]

I took a loan of 300,000. I am paying it over 3 years installments. The markup is 14%. The monthly fix payment would be 10,253. Can someone tell me how my bank calculated this monthly payment? You can see how the page below calculates it? http://www.realkerala.com/calc_emi.htm GB


View more questions Search