-1
public int[] doInBackground()
{
    int[] finalNumber = new int[2];
    finalNumber[0] = 0;
    finalNumber[1] = 0;
    int count = 0;
    int[] forDisplay = new int[2];

    outerLoop:
    for(int i = 1; i <= findUpTo; i++)
    {
        forDisplay[0] = i;
        forDisplay[1] = 2;
        publish(forDisplay);

        for(int a = 1; a <= i; a++)
        {
            if(isCancelled())
                break outerLoop; 
            else
            {
                try
                {
                    Thread.sleep(numGen.nextInt(5));
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }

                forDisplay[0] = a;

                if(i%a == 0)
                {
                    forDisplay[1] = 1;

                    count++;

                    if(count > finalNumber[1])
                    {
                        finalNumber[0] = a;
                        finalNumber[1] = count;
                    }
                }
                else
                {
                    forDisplay[1] = 0;
                }

                publish(forDisplay);
            }
        }
        setProgress(100*(i+1)/findUpTo);

        count = 0;
    }

    return finalNumber;
}

protected void process(List<int[]> publishedValues)
{
    for(int i = 0; i < publishedValues.size(); i++)
    {/*
        if(publishedValues.get(i)[1] == 1)
        {
            numbers.setFont(new Font("Serif", Font.ITALIC, 16));
        }
        else if(publishedValues.get(i)[1] == 2)
        {
            numbers.setFont(new Font("Serif", Font.BOLD, 18));
            numbers.append("\t");
        }*/

        numbers.append(publishedValues.get(i)[0] + "\n");
        //numbers.setFont(new Font("Serif", Font.PLAIN, 16));
    }
}

I'm not sure why this isn't working; if someone could help I'd appreciate it. I am trying to output forDisplay[0] in a JTextArea as it gets published, once. forDisplay[1] is used to determine the font in process (I commented the font changes out for now, they also were not working though it was going into the ifs in process [there was repeating text indentation])

The problem is that process seems to be appending the same number multiple times in a row and skipping others and I have no idea why.

The program itself is tasked with finding a number with the most divisors that produce no remainder from 1 to "findUpTo" (user input).

I'm rather new to SwingWorker so I'm having some difficulties understanding this issue.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

It’s hard for me to understand your code when it’s related to finding the most divisors, as you said. I will approach your problem more like an algorithmic question.

  1. We need to know the content of List object publishedValues. Simply post the contents of it in your question. The for loop in the process function is simple enough to work. However, I don’t like how you keep appending to the GUI object numbers without clearing, whatever that is.

  2. I find the code for finalNumber a bit confusing because of the several dependencies on “if( count > finalNumber..)" and the effect of it. My suggestion is to do the comparisons after you get all the divisors. Make debugging easier.

  3. Lastly, since I think this is an algorithmic problem, prototype your code without all this GUI and threading. I figure you’re only doing this for learning but it complicates things. There is a big appreciation in the software industry to keep things simple and less complicated than it could be. I challenge you to keep it simple!

The Original Android
  • 6,147
  • 3
  • 26
  • 31