Results 1 to 15 of 15
  1. #1
    owencain is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default Please help. Simple question

    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...");
    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


  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    Where is the hsa.Console class defined?

    What does the setCursor() method do?

  3. #3
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    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. #4
    owencain is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    what would i do to make the IF statement?

  5. #5
    owencain is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    is there any other way to display a variable's outuput without using c.print(ln)?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

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

    What does the setCursor() method do?

  7. #7
    owencain is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    it defines the grid layout

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #9
    owencain is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    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...");
            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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    Is the grid layout related to the problem?

  11. #11
    owencain is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    ya , put it in your compiler and you will see that the grid doesnt display correctly

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    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. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

    [Edit] 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. #14
    owencain is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    could you edit the pieces of code you are talking about and display them here? sorry its a little confusing

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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?

Similar Threads

  1. Simple question
    By Qsc in forum New To Java
    Replies: 6
    Last Post: 03-06-2011, 11:24 PM
  2. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 05:35 PM
  3. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 08:51 PM
  4. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 05:32 PM
  5. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 01:27 AM

Posting Permissions

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