• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple loop  RSS feed

 
Karthick Nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am new to Java.

My requirement is I want to show the output like

1
2
3
4
5
6
7
8
9
10

What is the code for this?? I mean I would assign a value for 'n'. For eg n=10 means I need to break up that into 2 pieces (5 in each) and then a couple of white spaces then next 5.....

Can anybody help me out...???

Thanks in advance....
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Karthick Nath wrote:
Can anybody help me out...???

What have you tried so far? You'll need to show your own code to get help.
 
Karthick Nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have tried that only. Please guide me.....
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks pretty good. What do you think is wrong with it? (I can see some minor things, but nothing major).

Also, UseCodeTags (← click) around your code. it helps readability. I've done it for you this time.
 
Karthick Nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to the output to be as follows

1
2
3
4
5
space
6
7
8
9
10

If the 'i' value is 20 means the output should be

1
2
3
4
5
space
6
7
8
9
10
space
11
12
13
14
15
space
16
17
18
19
20

Please guid as I hope you understand my requirement......
 
Karthick Nath
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried and achieved this of my own. Thanks for all your support who helped me out....



Output:
*********
1
2
3
4
5
Space
6
7
8
9
10
Space
11
12
13
14
15
Space
16
17
18
19
20
Space
21
22
23
24
25
Space
26
27
28
29
30
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to use more than one loop? Do you know how to use if statements?
You were not specific about your requirements. Do you mean printing a space character, or “space” or a blank line? Do you want a blank line after No 20?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weclome to the Ranch I added code tags to your post. Always use them: doesn't it look better

Only ever use \n if somebody tells you they want the LF character. You simply write println(). Actually println("\n") will not give the output you say it does. it gives
1
2
3
4
5


6 … with two blank lines after the 5.
There is a better way to write the if statement, saving you writing println(i) twice. I shall let you find it.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Karthick.

println() stands for print line. So if you use System.out.println(), it will print whatever you have passed inside the parenthesis and will add add a Line Feed(LF) character at the end of the line.

You can also use System.out.print() This will print whatever you have inside the parenthesis, but it won't add the Line Feed character at the end.

So if you use,

The output is going to be:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:Hello Karthick.

println() stands for print line. So if you use System.out.println(), it will print whatever you have passed inside the parenthesis and will add add a Line Feed(LF) character at the end of the line.

I'd be a little careful on that. Not all OS's use the same line separator. Some use a LF. some use a LF/CR. it is possible there are others, but each JVM knows what is appropriate for the OS it is running on.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!