• 06-16-2011, 12:13 AM
owencain
Alright to I have to make an algorithm and loop it to find all the prime numbers inbetween 0 and whatever, anyways, i have to have the output prime numbers come out in a grid/chart, I have it some what correct but whenever i run it, the higher the number i input, the lower the chart appears untill it is off the the page, as you will see when you compile it. I am using Java Ready to Program. im a noob. Please help me

Quote:

// The "Cain_Culminating" class.
import java.awt.*;
import hsa.Console;

public class Cain_Culminating
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();

int a, b, d, e, f, g, h, i, w, z;

c.println ("\t\t Welcome to my culminating program!");
c.println ("\t\t Please input the upper bound number...");
a = z + 1;
d = 0;
int gridrow = 8;
int gridcol = 1;
boolean[] array;
array = new boolean [a + 1];
for (f = 1 ; f < a ; f = f + 1)
{
d = 0;
for (b = 1 ; b < f ; b++)
{
e = f % b;
// c.println (e);

if (e == 0)
{
d = d + 1;
}
}
{
if (d > 1)
{
array [f] = false;

}
else if (d <= 1)
{
array [f] = true;

}

}
{
c.clear ();

for (w = 1 ; w < a ; w++)
{
if (array [w] == true)
{
c.setCursor (gridrow, 16 + (gridcol * 5));
c.println (w);
gridcol = gridcol + 1;
}
if (gridcol % 9 == 0)
{
gridrow = gridrow + 1;
gridcol = 1;
}
}
}
}

// Place your program here. 'c' is the output console
} // main method
} // Cain_Culminating class

• 06-16-2011, 12:36 AM
Norm
Where is the hsa.Console class defined?

What does the setCursor() method do?
• 06-16-2011, 12:47 AM
JDScoot
It may be do to a c.println(variableName) executing in a loop when the variableName is blank. In that case it would just keep adding a new line. the larger the number entered, the more times the loops iterated, the more c.println()'s are executed adding more new lines. Maybe an IF statement to make sure the variable isn't blank before running. c.println.
• 06-16-2011, 12:56 AM
owencain
what would i do to make the IF statement?
• 06-16-2011, 01:00 AM
owencain
is there any other way to display a variable's outuput without using c.print(ln)?
• 06-16-2011, 01:15 AM
Norm
Most people use: System.out.println(ln);

What does the setCursor() method do?
• 06-16-2011, 01:17 AM
owencain
it defines the grid layout
• 06-16-2011, 01:17 AM
pbrockway2
I'm not sure the code is correct near the end. Specifically you might be making gridrow bigger when you don't need to, but it's hard to tell with that crazy indentation.

Post the code indented as you see it. Don't use "quote" use the "code" tags instead. Put [CODE] at the start of the code and [/CODE] at the end; that way the indents will be preserved when the code appears here.
• 06-16-2011, 01:18 AM
owencain
Code:

```// The "Cain_Culminating" class. import java.awt.*; import hsa.Console; public class Cain_Culminating {     static Console c;          // The output console     public static void main (String[] args)     {         c = new Console ();         int a, b, d, e, f, g, h, i, w, z;         c.println ("\t\t Welcome to my culminating program!");         c.println ("\t\t Please input the upper bound number...");         z = c.readInt ();         a = z + 1;         d = 0;         int gridrow = 8;         int gridcol = 1;         boolean[] array;         array = new boolean [a + 1];         for (f = 1 ; f < a ; f = f + 1)         {             d = 0;             for (b = 1 ; b < f ; b++)             {                 e = f % b;                 //                c.println (e);                 if (e == 0)                 {                     d = d + 1;                 }             }             {                 if (d > 1)                 {                     array [f] = false;                 }                 else if (d <= 1)                 {                     array [f] = true;                 }             }             {                 c.clear ();                 for (w = 1 ; w < a ; w++)                 {                     if (array [w] == true)                     {                       c.setCursor (gridrow, 16 + (gridcol * 5));                       c.println (w);                       gridcol = gridcol + 1;                                                                 }                     if (gridcol % 9 == 0)                     {                     gridrow = gridrow + 1;                     gridcol = 1;                 }             }             }         }         // Place your program here.  'c' is the output console     } // main method } // Cain_Culminating class```
• 06-16-2011, 01:19 AM
Norm
Is the grid layout related to the problem?
• 06-16-2011, 01:20 AM
owencain
ya , put it in your compiler and you will see that the grid doesnt display correctly
• 06-16-2011, 01:34 AM
Norm
Quote:

What is the "it" you mention? I don't have your IDE or the Console class so I can NOT see what happens.

Is the setCursor() method necessary for your program?
Can you remove it and execute the program and see what happens.

If the setCursor() method is causing your problem, what is it doing wrong? What values are you giving it that are wrong?
Print out the values that you give the setCursor() method to see what the values are.
For example:
System.out.println("c.setCursor (gridrow, 16 + (gridcol * 5) gr=" + gridrow + " gc=" + gridcol );
• 06-16-2011, 01:36 AM
pbrockway2
Perhaps you should get rid of the braces except for those used with for and if/else. It would make the code easier to follow. (As would using descriptive variable names)

Why do you nest the two for loops? (put one inside the other) It means that the output is forever clearing the display and redrawing itself which seems like a waste of time. Another approach would be for the first for loop to populate array (give its elements values). And for the output code - from c.clear() onwards - to occur after that first loops has finished.

The second for loop - that one that does the output - should do nothing at all if array[w] is not true. At the moment you have it increasing gridrow too often: gridrow should only increase just after gridcol has been changed.

 Many folk don't have this hsa pacakge and so can't run your program. And many of the rest wouldn't anyway: they will rely on the code itself and your description of the problem being intelligible.
• 06-16-2011, 01:40 AM
owencain
could you edit the pieces of code you are talking about and display them here? sorry its a little confusing
• 06-16-2011, 02:07 AM
pbrockway2
It might be easier to deal with one confusion at a time. Concerning the readability: did you understand what I meant by not having braces except associated with for loops and if/else statements?