aspose file tools*
The moose likes Beginning Java and the fly likes tricky program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "tricky program" Watch "tricky program" New topic
Author

tricky program

Sam PaulD
Greenhorn

Joined: Apr 02, 2008
Posts: 2
One of my friends posed me a question i foud it to be bit tricky can any one help me in this

Question goes like this :




the obvious out put is Sam! but wants out put like this "Hai Sam! Bye." without changing the main method can we do this ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

For executing something before the main method is executed you can use a static initializer block. That still does not help you with the output after the main method's code.

I can cheat however:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sam PaulD
Greenhorn

Joined: Apr 02, 2008
Posts: 2
I guess you are missing some part of the code I am getting compilation errors at


can you pl. re check it
mahesh kale
Greenhorn

Joined: Apr 02, 2008
Posts: 6
i think that code works ine but i did few modifications with it...will post it..as soon as i get my eclipse window out of CTRL ALT DEL
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by Sam PaulD:
I guess you are missing some part of the code I am getting compilation errors at


can you pl. re check it

You'll have to import java.io.PrintStream.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
@ Rob Prime

Can you please explain the code especially the Print stream ??


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
@ROb Prime.
Thanks a lots mate...
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
What about just a simple print statement?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Well, you can also stop the main() method from executing with this static initializer.



Too bad the main methods uses the println() method, instead of the print() method. If it just used the print() method, you could've also done this in the static initializer.



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by Irina Goble:
What about just a simple print statement?

It misses the "Bye." part.
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
Originally posted by Rob Prime:

It misses the "Bye." part.


Oops, missed it! It will print the " Bye." part if it's called as
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tricky program