File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Printing a String object without using loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing a String object without using loop" Watch "Printing a String object without using loop" New topic
Author

Printing a String object without using loop

Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Can we print the String "Hello World!" 1000 times without using loop?


dkj4jdk
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10255
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

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

Joined: Oct 02, 2003
Posts: 11230
    
  16

recursion

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Thanks Fred, but a code snippet will be more helpful.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Oct 02, 2003
Posts: 11230
    
  16

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

Joined: Oct 12, 2001
Posts: 1179

Yeah, the good old "xx Bottles of beer on the wall" song

Regards, Rene Larsen
Dropbox Invite
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

That's cute, Fred!


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

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

Joined: Oct 13, 2005
Posts: 38363
    
  23
Deepak Kumar Jena wrote: . . . a small piece of code . . .
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

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

Joined: Oct 23, 2009
Posts: 150

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


star chaser..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Recursion. With that link, Gary's statement and Fred's post you should really be able to solve this yourself.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Oct 02, 2003
Posts: 11230
    
  16

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

Joined: Sep 08, 2010
Posts: 68

Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Thanks to all. I have done it.
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Thanks to all for your valuable suggestions.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Great job man.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing a String object without using loop