Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing a String object without using loop

 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we print the String "Hello World!" 1000 times without using loop?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write System.out.println("Hello World!"); 1000 times in your code?
What problem are you trying to solve here. If you can provide some more information, we can get a clearer picture.
 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let, String msg="Hello World!"; Can we print this message for 1000 times without using any loop? I have faced this question in an interview and replied exactly the same as you. But the interviewer asked me to search for it in google.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
recursion
 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred, but a code snippet will be more helpful.
 
Campbell Ritchie
Sheriff
Posts: 48407
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Kumar Jena wrote:Thanks Fred, but a code snippet will be more helpful.
As they say here in Teesside, "Thereygo".
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Kumar Jena wrote:Thanks Fred, but a code snippet will be more helpful.

We generally don't just give out answers. Too many folks would be tempted to use us to cheat on their homework.

do you know what recursion is? Have you ever written a recursive function? It's a function that calls itself, each time reducing to a simpler case.

Printing something 1000 times is easy. you just print it once, and then call a function that prints it 999 more times.
Printing something 999 times is easy. you just print it once, and then call a function that prints it 998 more times.
Printing something 998 times is easy. you just print it once, and then call a function that prints it 997 more times...
...
Printing something 2 times is easy. you just print it once, and then call a function that prints it 1 more time.
Printing something 1 time is easy. you just print it once, and then don't call any other function.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the good old "xx Bottles of beer on the wall" song
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's cute, Fred!
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Deepak Kumar Jena wrote:Thanks Fred, but a code snippet will be more helpful.

We generally don't just give out answers. Too many folks would be tempted to use us to cheat on their homework.

do you know what recursion is? Have you ever written a recursive function? It's a function that calls itself, each time reducing to a simpler case.

Printing something 1000 times is easy. you just print it once, and then call a function that prints it 999 more times.
Printing something 999 times is easy. you just print it once, and then call a function that prints it 998 more times.
Printing something 998 times is easy. you just print it once, and then call a function that prints it 997 more times...
...
Printing something 2 times is easy. you just print it once, and then call a function that prints it 1 more time.
Printing something 1 time is easy. you just print it once, and then don't call any other function.

Let's make it a bit harder, going in a similar direction.

Printing something 1000 times is easy. You print it 500 times twice.
Printing something 500 times is easy. You print it 250 times twice.
...
Printing something 125 times is a bit harder but still easy. You print it once, then two times 62 times. Or you print it 63 times and 62 times.
...
Printing something 1 time is easy. You print it and you're done.


Note that Fred's example is easier, and both are just as fast. After all, in the end they will both do the same thing: print something 1000 times. I just wanted to show that even with recursing there can be alternatives.
 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys for the answers and suggestions. What I think is explaining the logic to work with and a small piece of code will help to the inexperienced programmers.
 
Campbell Ritchie
Sheriff
Posts: 48407
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Kumar Jena wrote: . . . a small piece of code . . .
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what would help even more would be for you to try doing it yourself. One of two things will happen:

1) You'll get it to work (HORAY!!!)
2) You'll get stuck, and then you can post your code here and we can make suggestions from that.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
recursion - a function that calls itself.

I wonder if programmers create an infinite recursion and what is use for? Just a thought that came up when I saw this posting.

Gary
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recursion. With that link, Gary's statement and Fred's post you should really be able to solve this yourself.
 
Campbell Ritchie
Sheriff
Posts: 48407
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Ba wrote: . . . I wonder if programmers create an infinite recursion and what is use for? . . .
Of course people create infinite recursion. It is very useful. You can kick the computer, shout, "The bl**d* thing will never work," and then, most important of all . . .












































. . . go for a cup of coffee!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Ba wrote:I wonder if programmers create an infinite recursion and what is use for?

yes they do it, but usually not on purpose. in programming, each call should be to a simpler case. eventually, you should get to some 'base' case that doesn't need to be simplified any more.

If you code it wrong (and probably everyone who has ever tried to write a recursive function has done this), you don't reduce to your base case. Since each call uses a little more memory (and doesn't free the memory already used), you can quickly run out. At that point, the JVM will throw and out-of-memory exception and your program crashes. This is generally considered a 'bad thing', and people try to avoid it.
 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all. I have done it.
 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for your valuable suggestions.
 
Stephan van Hulst
Bartender
Pie
Posts: 5387
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great job man.
 
Campbell Ritchie
Sheriff
Posts: 48407
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can improve that no end. Apart from the indentation . . .

You don't need the return;. You simply put the entire body of the method inside an if (counter > 0) { . . . } block. You are better using counter - 1 than --counter . Since counter is a local variable, you are not accessing a field. Since that method does not access any fields, it could be made static.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code currently has one danger - if counter < 0 your program will print out the string until the counter is Integer.MIN_VALUE (- 2^31), then print with a counter of Integer.MAX_VALUE (2^31 - 1), then print out until counter == 0. The check Campbell suggested (if (counter > 0)) will solve this issue for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic