DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: How can I solve this problem?

1. Registered User
Join Date
Sep 2004
Posts
9

## How can I solve this problem?

Hello,

I have an array which contains 6 randomly generated numbers (which is dealt with in a method). What I want to do is display these numbers in a row, as shown below:

7 12 14 15 19 44

How would I go about achieving this please? I would also like numbers in the range 1 to 9 to be proceeded by a blank space (in order to keep the numbers aligned if I wish to do more than one row of numbers).

I tried to achieve this using a for loop and System.out.println as shown below, however the numbers just appeared one after the other.

Code:
```for(int i = 0; i < SIZE; i++) {
System.out.println(number[i]);
}```
Any help would be greatly appreciated, thank you.

2. Registered User
Join Date
Jan 2005
Posts
38
Hello my friend

you can use this two lines:

Code:
```for(int i=0;i<SIZE;i++)
System.out.print(number[i]+" ");```
your mistake that you are use println() >>> which print and go to new line >>>

3. Registered User
Join Date
Mar 2004
Posts
20

## this should do it!

if u want to add a space after any numbers in the range 1 to 9, u will need to use an if statement, try sumthing as follow's:

Code:
```for(int i = 0; i < SIZE; i++){
if(number[i] > 0 && number[i] < 10){
System.out.print(number[i]+"  ");   //double space
}
else{
System.out.print(number[i]+" ");   //Single Space
}
}```

4. Registered User
Join Date
Sep 2004
Posts
9
Thanks for the help !

I wish to proceed the number 1 to 9 with a blank space, not neccessarily put a space after them.

So in theory it would look something like this (minus the 0's)

07 14 16 17 08 23
12 03 34 23 39 30

If you see what I mean, they are all in line (I just don't want the 0 before each number between 1 and 9)

Any ideas please, thanks for the help so far.

5. Senior Member
Join Date
Feb 2004
Posts
541
for(int i = 0; i < SIZE; i++){
if(number[i] > 0 && number[i] < 10){
System.out.print(" " + number[i]+" "); //double space
}
else{
System.out.print(number[i]+" "); //Single Space
}
}
Try this....if the number is single digit it prints SPACE + NUMBER + SPACE. If it's a double digit number, it doesn't print the first space.

6. Registered User
Join Date
Sep 2004
Posts
9
Thank you, how could I make it so that if it is the last number that it does a 'return' (i.e. it proceeds down to the next line ready for the next set of numbers) please?

7. Registered User
Join Date
Mar 2004
Posts
20

## this should do it!

basically u just want to go to the next line after you've done going thru the array... try this:
Code:
```   for(int i = 0; i < SIZE; i++){
if(number[i] > 0 && number[i] < 10){
System.out.print(" " + number[i]+" "); //double space
}
else{
System.out.print(number[i]+" "); //Single Space
}
}
System.out.print("\n");   //should take it to the next line, without
//without leaving a line space inbetween```
hope that helps

8. Registered User
Join Date
Sep 2004
Posts
9
Works well, thank you very much.

9. Registered User
Join Date
Mar 2004
Posts
20
You're welcome

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise