# Java Help Needed Please



## pindi (Jun 11, 2006)

I was wondering how i would do this?

int variable k that has already been declared, use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k

I have done this and its not working

while (k > 88) {
System.out.print('*');
k = k - 1;
}

Can someone help me please...i appreciate it


----------



## Sideshow_Bob (Jul 5, 2005)

are you initializing k to 177?

Why not make it simple and do the following:
int k = 0;
while (k < 88)
{
System.out.print("*");
k++;
}

Is k initialized to something that you cannot change?


----------



## pindi (Jun 11, 2006)

Hey Sideshow_Bob,
Thanks for replying..
to answer your question, int k is already declared and not initializing to 177..
here is the code so far..

public class CTest {

public static void main(String[] s) {
int k;
while (k < 88)
{
System.out.print('*');
k++;
}
}
}

and i got an error msg of "variable k might not have been initialized" which doesnt make any sense to me...
any help would be appreciated..thanks again


----------



## Sideshow_Bob (Jul 5, 2005)

Ok, you have done nothing wrong. The problem is that the program you are coding in makes sure that you have initialized all variables before you use them. Most do. I run into this all the time. All you need to do is change

int k;

to 

int k = 0;

The program does this to try to avoid errors from occuring. For instance you may declare an Object and then try using one of the Object's methods on it sometime later on during execution. The problem is however that, unless you have given that object a reference to something, the object would be null and you would get a null pointer exception. The program tries to avoid this by making sure that you always give an initial value to all variables. Even if that value is null anyway.


----------



## Chicon (Jul 29, 2004)

You may also use :


```
[B][SIZE=3]

for (int k = 0; k < 88; k++) {
....
}[/SIZE][/B]
```


----------



## pindi (Jun 11, 2006)

thanks so much for the help..i really appreciate it


----------

