hey i'm trying to do my homework for a computer science class, and im a bit stuck. When i run the program, the terminal window doesn't even show up, can anyone help me please. Thanks in advance.

class:

public class Grid
{
    public Grid()
    {
    rows = 10;
    columns = 10;
    }
    public String draw()
    {
        String r  = "";
         for(int i = 1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++)
                        r = r + " _";
                        r   = r + "\n";            
                }
                return r;               
}
private double rows;
private double columns;
}

driver:

public class Driver
{
    public static void main (String[] args)
    {
        Grid test = new Grid();
        for(int k =0; k ==100; k++)      
        {
        System.out.println(test.draw());
        System.out.print("\n\n");
    }
}
}

Recommended Answers

All 6 Replies

Start with this change:
from

for(int k =0; k ==100; k++)

to

for(int k =0; k < 100; k++)

Start with this change:
from

for(int k =0; k ==100; k++)

to

for(int k =0; k < 100; k++)

wow i feel dumb, thx alot

for(int i = 1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++)
                        r = r + " _";
                        r   = r + "\n";            
                }

Your nested "if" statement's statements are not defined correctly, and/or your indenting is problematic. I BELIEVE your code should look like this:

for(int i = 1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++) {
                                r = r + " _";
                                r   = r + "\n";      
                        }
                }
for(int i = 1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++)
                        r = r + " _";
                        r   = r + "\n";            
                }

Your nested "if" statement's statements are not defined correctly, and/or your indenting is problematic. I BELIEVE your code should look like this:

for(int i = 1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++) {
                                r = r + " _";
                                r   = r + "\n";      
                        }
                }

You couldn't be more wrong.


>>>>> Shotty, your code is correct.


And tjsail try running what you wrote and what Shotty wrote and see the difference. Don't give wrong advices without testing them or running the poster's code

My apologies, but please take note to the fact that i said that i believe his code should look like this. the way i was taught for loops says that you should include brackets to enclose the body of the loop otherwise it will not function correctly. my apologies for trying to help.

TJ

My apologies, but please take note to the fact that i said that i believe his code should look like this. the way i was taught for loops says that you should include brackets to enclose the body of the loop otherwise it will not function correctly. my apologies for trying to help.

TJ

Then this would be correct:

for(int i = 1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++) {
                                r = r + " _";
                        }
                        r   = r + "\n";      
                }

Your error was a logical one, the code would compile but not do what it is suppose to do. You were taught to use brackets but be careful where you put them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.