Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Java Help Please
#1

hai guys here's a program









public class

{

public static void main(String[ ] args)

{

System.out.println("Sam! ");

}

}



i want the output as "Hai Sam! Bye" with out changing the main method do any1 of u know it.........

[size="6"]Beckyard[/size]
Reply
#2

it is not possible without changing main
Reply
#3

You should change small thing in the program then u wil get above output..



public class class1

{

public static void main(String[ ] args)

{

System.out.println(args[0]+"Sam! "+args[1]);

}

}



Now Compile Your Program:



javac class1.java



java class1 "Hai" "How r u??"
Reply
#4

here you go,



public class dexp {

static {

System.out.print("Hi ");

main(null);

System.out.print("Bye");

System.exit(0);

}

public static void main(String[ ] args) {

System.out.println("Sam! ");

}

}



only thing I could not do was going back to previous line before printing bye. If you change println to print then it would print in the same line, however another solution would be to avoid calling main(). But in that case solution does not seem to be genuine.
Reply
#5

import java.io.*;
 
class A{
public static void main(String[] ar) throws Exception{
System.setOut(new PrintStream(System.out){
@override  
 public void print(String x){
   super.print("Hello "+x+" Bye ");
}
@override  
public void println(String y){
super.println("Hello "+y+" today is "+new java.util.Date());
}
});
System.out.println("Saurabh");
System.out.print("Soham");
}
}
------------------------------------------------------
For more interesting things, students may log on to http://sohamsironline.weebly.comand [url=]http://sohamsironline.ucoz.com [/url]
Reply
#6

import java.io.*;
 
class A{
public static void main(String[] ar) throws Exception{
System.setOut(new PrintStream(System.out){
@override  
 public void print(String x){
   super.print("Hello "+x+" Bye ");
}
@override  
public void println(String y){
super.println("Hello "+y+" today is "+new java.util.Date());
}
});
System.out.println("Saurabh");
System.out.print("Soham");
}
}
------------------------------------------------------
For more interesting things, students may log on to http://sohamsironline.weebly.comand [url=]http://sohamsironline.ucoz.com [/url]
Reply
#7

import java.io.*;
 
class A{
public static void main(String[] ar) throws Exception{
System.setOut(new PrintStream(System.out){
@override  
 public void print(String x){
   super.print("Hello "+x+" Bye ");
}
@override  
public void println(String y){
super.println("Hello "+y+" today is "+new java.util.Date());
}
});
System.out.println("Saurabh");
System.out.print("Soham");
}
}
------------------------------------------------------
For more interesting things, students may log on to http://sohamsironline.weebly.comand [url=]http://sohamsironline.ucoz.com [/url]
Reply
#8

import java.io.*;
 
class A{
public static void main(String[] ar) throws Exception{
System.setOut(new PrintStream(System.out){
@override  
 public void print(String x){
   super.print("Hello "+x+" Bye ");
}
@override  
public void println(String y){
super.println("Hello "+y+" today is "+new java.util.Date());
}
});
System.out.println("Saurabh");
System.out.print("Soham");
}
}
------------------------------------------------------
For more interesting things, students may log on to http://sohamsironline.weebly.comand [url=]http://sohamsironline.ucoz.com [/url]
Reply
#9

import java.io.*;
 
class A{
public static void main(String[] ar) throws Exception{
System.setOut(new PrintStream(System.out){
@override  
 public void print(String x){
   super.print("Hello "+x+" Bye ");
}
@override  
public void println(String y){
super.println("Hello "+y+" today is "+new java.util.Date());
}
});
System.out.println("Saurabh");
System.out.print("Soham");
}
}
------------------------------------------------------
For more interesting things, students may log on to http://sohamsironline.weebly.comand [url=]http://sohamsironline.ucoz.com [/url]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)