0

Would some one be able to help me please?

If I have a class like this

public class a {

public String b (String c){
String d = "e";
return d;

}

}

When I call a f = new a();

f.b();

I'm unable to have the string d returned. I get the error "cannot be applied to ()"

I'm sure I'm doing something stupid but I cant work it out.

2
  • Are you using c in your method? Commented Mar 5, 2011 at 16:46
  • 1
    if any of the answers below have given you the solution please accept one of them. Otherwise provide some more information and I'm sure the community will continue to try and aid you. Commented Mar 5, 2011 at 17:07

8 Answers 8

6

You have

public String b (String c){

but call b() without any parameter. That's what the error wants to tell you...

Sign up to request clarification or add additional context in comments.

Comments

3

method b takes a parameter. so try

f.b("some string c");

1 Comment

I'm passing in a string from another class, is what I'm doingnot possible?
2

Your method b requires a String to be passed into it.

When you call f.b() it looks for a method with a signature similar to

public String b(){
 // your code
}

2 Comments

I'm passing in a string from another class, is what I'm doingnot possible?
According to the code you've given us you are not passing a string at all.
2

You have to call like

f.b(aStringVariable);

or

f.b("Some String");

You have to pass the variable while calling the function.

Comments

2

Add ... to your method parameter declaration. This will make String parameter optional.

public String b (String... c){
  String d = "e";
  return d;
}

and then new a().b().

Comments

1

When you call f.b() you're not passing a string to the method. You declared your function as public String b(String c). That means you have to pass a string when you call f.b.

If you change you declaration to public String b() you do not have to pass a string. Another solution is simply passing a string, i.e f.b("a string").

Comments

1

You are passing String c into your b method

public String b (String c)

you are getting that error because there needs to be a string variable like

a f = new a();  f.b(c);

Where c is some predefined string.

Comments

0

Use some string parameter and it will work: f.b("String")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.