Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use custom System1.out1.println1?  RSS feed

 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i change System.out.println to System1.out.1println1 and use this new syntax??
 
Cameron Miller
Greenhorn
Posts: 13
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't change the syntax of java.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no different syntax there - it's just got different identifiers. You can create a class that's got a println1 method (1println1 is out, because identifiers can't start with a digit). You can create another class called System1, which has a static public variable called out, with the type of the first class. You can make these do whatever you want, so you could certainly have the method end up delegating to System.out.println.

But it's almost certainly a bad idea. What are you actually trying to achieve?
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you actually trying to achieve?


Hi Matthew and Cameron, thank you for your reply.
Matthew,I too had a similar idea but was unable to put it down to implementation. I wanted to replace System.out.println with my own custom tags just to better understand their functionality.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivom Shukla wrote:
What are you actually trying to achieve?


Hi Matthew and Cameron, thank you for your reply.
Matthew,I too had a similar idea but was unable to put it down to implementation. I wanted to replace System.out.println with my own custom tags just to better understand their functionality.


The first questions you have to ask yourself, and answer, are:

1. What is System?

2. What is out?
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,
Yes you are right...will do.
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried This but still no where near result
What am i doing wrong. Sorry i am not a good java programmer...but trying to improve ...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. When posting code, UseCodeTags(←click) so it will be easier to read. I added them for you this time, and it helps a little, but your indentation is still kind of weird.

2. ItDoesntWorkIsUseless(←click). You need to TellTheDetails(←click) about exactly what problem you're having.
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

Thank you for correcting my mistakes.

The problem with this code is that i am getting two errors at compile time as follows:

1) sys2.java:11: cannot find symbol
symbol : method write(java.lang.Object)
location: class PrintStream
write(o);
^
2) sys2.java:18: cannot find symbol
symbol : variable out1
location: class System1
System1.out1.println1("Hello Java");

Conclusion: I am not able to replace System.out.println() with System1.out1.println1()
 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your System1 class needs a public static variable (not a method) called out1 which is of type MyPrintStream (or whatever you want to call it). MyPrintStream needs to implement a println1 method which calls System.out.println() to output the String to the screen. Although I'm at a loss as to why you want to do this, can you explain what you are trying to achieve.
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,

MyPrintStream needs to implement a println1 method which calls System.out.println() to output the String to the screen.


Can you explain which String is required in output?

I am trying to use System1.out1.println1 because one of my seniors have asked me to write it as a challenge.
I am unable to do so and still trying. Will surely follow your advice and post my code as soon as I finish.

Thanks,
Shivom
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my code....

 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done.
I wouldn't call the PrintStream class "PrintStream" though as there is already a standard Java class with this name. This won't stop your code from working (provided you don't accidentally add an import statement for the Java class with this name) but it can be confusing so I'd give it a unique name.
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Tony,

Yes you are right... I will change it to,say, MyPrintStream..

Regards,
Shivom
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!