1. Member
Join Date
Jun 2011
Posts
12
Rep Power
0

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

// 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

2. Where is the hsa.Console class defined?

What does the setCursor() method do?

3. Member
Join Date
Jun 2011
Location
San Diego, CA
Posts
24
Rep Power
0
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.

4. Member
Join Date
Jun 2011
Posts
12
Rep Power
0
what would i do to make the IF statement?

5. Member
Join Date
Jun 2011
Posts
12
Rep Power
0
is there any other way to display a variable's outuput without using c.print(ln)?

6. Most people use: System.out.println(ln);

What does the setCursor() method do?

7. Member
Join Date
Jun 2011
Posts
12
Rep Power
0
it defines the grid layout

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,546
Rep Power
11
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.

9. Member
Join Date
Jun 2011
Posts
12
Rep Power
0
Java 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...");
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```

10. Is the grid layout related to the problem?

11. Member
Join Date
Jun 2011
Posts
12
Rep Power
0
ya , put it in your compiler and you will see that the grid doesnt display correctly

12. put it in your compiler
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 );

13. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,546
Rep Power
11
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.
Last edited by pbrockway2; 06-16-2011 at 12:38 AM.

14. Member
Join Date
Jun 2011
Posts
12
Rep Power
0
could you edit the pieces of code you are talking about and display them here? sorry its a little confusing

15. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,546
Rep Power
11
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?

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•