This is program that return a concatenated string to the main()

import java.io.*;

class ConCat {
public static void main(String args[]) {
    String a, b ,c;
    a = "Animesh ";
    b = "Pandey";
    c = concat(a, b);
    System.out.println(c);
}
}


static String concat(String s1, String s2) {
   String C;
   C = s1 + s2;
   return C;
}

List of errors !!!

F:\java\example>javac ConCat.java
ConCat.java:1: class, interface, or enum expected
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
 ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
      ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
           ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
                       ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
                             ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
                                           ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
                                                    ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
                                                       ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
                                                               ^
ConCat.java:1: illegal character: \92
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}
                                                                     ^
ConCat.java:1: class, interface, or enum expected
{\rtf1\ansi\ansicpg1252\deff0\deflang16393{\fonttbl{\f0\fmodern\fprq1\fcharset0
Courier New;}}

            ^
ConCat.java:2: illegal character: \92
{\*\generator Msftedit 5.41.21.2506;}\viewkind4\uc1\pard\f0\fs24 import java.io.
*;\par
 ^
ConCat.java:2: illegal character: \92
{\*\generator Msftedit 5.41.21.2506;}\viewkind4\uc1\pard\f0\fs24 import java.io.
*;\par
   ^
ConCat.java:2: class, interface, or enum expected
{\*\generator Msftedit 5.41.21.2506;}\viewkind4\uc1\pard\f0\fs24 import java.io.
*;\par
                                    ^
ConCat.java:2: illegal character: \92
{\*\generator Msftedit 5.41.21.2506;}\viewkind4\uc1\pard\f0\fs24 import java.io.
*;\par
                                     ^
.......

Please help me remove them, file name is ConCat.java.

U r code contains sillly mistake..

remove the close bracket after the main method and place it at the end..

U r code contains sillly mistake..

remove the close bracket after the main method and place it at the end..

import java.io.*;

class ConCat {

static String concat(String s1, String s2) {
   String C;
   C = s1 + s2;
   return C;
}

public static void main(String args[]) {
    String a, b ,c;
    a = "Animesh ";
    b = "Pandey";
    c = concat(a, b);
    System.out.println(c);
}
}

But sir I am still getting the same list of errors ...... !

Use plain text editor.

Use plain text editor.

To be little more descriptive, Microsoft Office Word is not recommended package to write your code. Use either simple editors like Notepad or get some IDE like IntelliJ IDEA, Eclipse, NetBeans, JCreator, BlueJ etc.

Member Avatar for ztini

I'm curious though, why would you implement a concat method? What's the benefit of that over simply doing this:

public static void main(String[] args) {
		String a = "Animesh ", b = "Pandey";
		System.out.println(a + b);
	}

It is actually good practice as not to over-crowd single method with unrelated stuff. Plus you never know when you may need to call same method again. That is just for start to considerate.

Member Avatar for ztini

I was more asking why create a method to concat strings when the class itself already provides this method. I completely agree about breaking out code for reuse elsewhere, but I disagree with "reinventing the engine" so-to-speak.

Let's look at it:

String c = ConCat.concat(a,b);

vs

String c = a + b;

I'm not sold.

If you're going into the wheel-building business, it makes sense to invent some wheels. If that means re-inventing them, so be it.

And besides, re-inventing the engine seems like a good idea. Car companies do it all the time...

If you just want a wheel for your wagon or an engine for your go-cart, it's generally better to order one from a catalog and get on with your real work, but that doesn't mean it's not good to think about how they work - either to make a better one, or to know how to use them better.

Member Avatar for ztini

Without digressing too much here, I agree invent and re-invent if there is a benefit. What's the benefit of re-inventing String concatenation? Although, you would get to thoroughly confuse developers who look at your code later. Oh I know, this is perfect if you get paid by the keystroke -- lots of extra pointless code! Money! Money! :P

If any developers are looking at this after the code is written, you're absolutely right. Somehow, I don't think the original poster is on a full-bore development team just yet... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.