Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

invalid method declaration, return type require

Status
Not open for further replies.

bostero

Programmer
Apr 17, 2008
3
CA
public String(String num){
if (num == null) {
return "N/A";
}
int len = num.length();
int c = 0;
char[] sb = new char[len];
for (int i = 0; i < len; i++) {
sb[c++] = num.charAt(i);
if ((len - 1 - i) % 3 == 0 && i != len - 1) {
sb[c++] = ',';
}
}
return new String(sb);
}
}
This is the code and I can not find why it is giving me that error when compiling
 
The name of your method is "String". That, in and of iteslf is a bad idea. You do not specify what type of data is returned (as in public String MethodName(args) {}), yet you return a String ( return new String(sb);).

_________________
Bob Rashkin
 
how about is I do this
public class Main{
public String name(String num) {
if (num == null) {
return "N/A";
}
int len = num.length();
int c = 0;
char[] sb = new char[len];
for (int i = 0; i < len; i++) {
sb[c++] = num.charAt(i);
if ((len - 1 - i) % 3 == 0 && i != len - 1) {
sb[c++] = ',';
}
}
return new String(sb);
}
}
 
That looks ok but naming a method "name" is probably an invitation to problems, too.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top