File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

concat 2 strings using constructor

 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone i am new to java was just doing practice ... i have this assignment can you please help how to do it..
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ! We have a policy at the ranch to let you DoYourOwnHomework. What do you have so far ?
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii Ok. I have written something like this is it correct now how can i input 2 strings
 
Vijitha Kumara
Bartender
Posts: 3909
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amrut sabade wrote:Hii Ok. I have written something like this is it correct now how can i input 2 strings

You have written a method called "Main" (which happens to be the name as your class, which is not good). You should pick meaningful names for your methods. OK, to call the method you should create an instance of your class and call the method by passing the required parameters.

Ex:

 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Bt it is constructor so it has the same name as class.. now i want to pass to strings to the constructor..aand display the output..so how can i pass 2 strings to constructor.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor can have arguments like any other method, just change the constructor's signature to the one resembling public String Main - but remember that constructors don't return values.
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.. can i cal constructor from main as method? passing arguments
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could easily have tried this one. Yes, you can, but from main, not Main.
As Vijitha pointed out, you have a method with the same name as your class. You argued that this is a constructor, but it isn't. Have a closer look at it.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What everyone is trying to wanr you is that the entry point of a Java program is a class having a method called "public static void main(String[] args)". If you name your class Main, and make constructors in this class, this may be confusing :


As Jan said, constructors do not return values, so you cannot have public String Main(String a,String b).

Can you tell us exactly what the content of your assignment is ?I feel that some information is missing.
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right
it shoud nt have return type and should not return value..

is correct now? can i cal it from main
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost - if you rename your Class to StrMain so that it has the same name as your constructor it is correct, and you can call it from main
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my assignment is WAP that concats the two strings using constructor and displays the output
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what WAP is, but if you don't need to return the value, then your last code looks fine. You could use a+b instead of calling the concat method too.
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii now i have written this program and when i try to create object of the my Concat class it gaves me error
cannot find symball constructor concat
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you are hiding the default constructor with your Concat(String a,String b) constructor. But that's not the real problem. The real problem is that you have to pass strings to your constructor. Like you would with a method having parameters. Can you figure out how ?
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.. i understod it but i want to do it using constructor only.. thaths the prob i can write method and do it..
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey finally i have done with the assignment
Thanks a lot Guys Vijitha Kumara,Christophe Verré ,Jan Hoppmann ..... withou your help it woudent be possible... really thanks a lot and looking forward to your help in the way of learning
FINALLY MY PROG IS AS BELOW..
 
amrut sabade
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys can you tell me sompe sample programes that i should try to implement so that i should get my bascis clear...
can you help me ...
 
Vijitha Kumara
Bartender
Posts: 3909
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Codingbat got some good exercises. But most of these require that you know the basics well and can apply them.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic