wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes concat 2 strings using constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "concat 2 strings using constructor" Watch "concat 2 strings using constructor" New topic
Author

concat 2 strings using constructor

amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Hi ! We have a policy at the ranch to let you DoYourOwnHomework. What do you have so far ?


[My Blog]
All roads lead to JavaRanch
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
Hii Ok. I have written something like this is it correct now how can i input 2 strings
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

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:


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
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

Joined: Jul 19, 2010
Posts: 147

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.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
Ok.. can i cal constructor from main as method? passing arguments
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Jul 19, 2010
Posts: 147

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

Joined: Aug 16, 2010
Posts: 90
my assignment is WAP that concats the two strings using constructor and displays the output
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Mar 24, 2008
Posts: 3833

Codingbat got some good exercises. But most of these require that you know the basics well and can apply them.
 
GeeCON Prague 2014
 
subject: concat 2 strings using constructor