0

I can't figure out why I get this error.

Here is the code:

public static int WriteBirdType() {

BufferedReader reader = new BufferedReader( new FileReader ("fugler.txt"));
String         line = null;
StringBuilder  stringBuilder = new StringBuilder();
String         ls = System.getProperty("line.separator");

while( ( line = reader.readLine() ) != null ) {
stringBuilder.append( line );
stringBuilder.append( ls );
}

return stringBuilder.toString();

Here is the error:

test.java:78: error: incompatible types
return stringBuilder.toString();
                             ^
required: int
found:    String
1 error

What can I do to fix this?

3
  • Your method has a return type of void, ie. nothing. You can't return anything as it stands. Commented Sep 17, 2013 at 22:39
  • Was supposed to say int, edited it now, i get this error when its written int and not void. Commented Sep 17, 2013 at 22:41
  • If you're returning "String", then your signature should say "String" ;) Commented Sep 17, 2013 at 22:42

2 Answers 2

5

You are attempting to return a String when you declared the method to return int. Either return an int, or change the return type of the method to String.

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

Comments

4

Your return type must match the method signature.

If your return type is int, then you need to return and int, not a string. toString() returns a string. If you need to return a String, then change the method signature to String, not int.

Do this:

    //change return type to string
    public static String WriteBirdType() {

    BufferedReader reader = new BufferedReader( new FileReader ("fugler.txt"));
    String         line = null;
    StringBuilder  stringBuilder = new StringBuilder();
    String         ls = System.getProperty("line.separator");

    while( ( line = reader.readLine() ) != null ) {
    stringBuilder.append( line );
    stringBuilder.append( ls );
    }

     return stringBuilder.toString();

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.